Search code examples
excelvba

Cannot write Value in Cell.Throws Error 1004


I want to copy Data from one Worksheet to another. When Setting the value of the destination cell i get the Error 1004

Sub FillSheets()

Dim MainSheetCell As Range
Dim DeviceSheetCell As Range

Set DeviceSheetCell = Worksheets("Device-Label").Range("A2")
Set MainSheetCell = Worksheets("MAA#Kennzeichnung").Range("B5")

Dim AnlagenName As String
AnlagenName = "==" & Range("B2").Value
 
'Checking for first empty column
Do
    If MainSheetCell.Value = "" Then Exit Do
    
    'Is the Device placed in the field?
    If MainSheetCell.Offset(0, 1) = "+X" Then
        Dim Buffer1 As Variant
        Dim Buffer2 As Variant
        Dim Buffer3 As Variant
        Dim Buffer4 As Variant
        
        Buffer1 = MainSheetCell.Offset(0, -1).Value
        Buffer2 = MainSheetCell.Value
        Buffer3 = MainSheetCell.Offset(0, 2).Value
        Buffer4 = AnlagenName & "-" & MainSheetCell.Value
        Buffer1 = "Test"
        
        Set DeviceSheetCell = Worksheets("Device-Label").Range("A2")
        DeviceSheetCell.Value = Buffer1
        DeviceSheetCell.Offset(0, 1).Value = Buffer2
        DeviceSheetCell.Offset(0, 2).Value = Buffer3
        DeviceSheetCell.Offset(0, 3).Value = Buffer4
    End If
   
    Worksheets("MAA#Kennzeichnung").Activate
    ActiveCell.Offset(1, 0).Activate
Loop

End Sub

The Error is thrown in the Line DeviceSheetCell.Offset(0, 1).Value = Buffer2 Buffer 1 with my Test string works just fine.

The read data looks fine. The writing also works with my Test String. Changing the Buffer Data Type to String does not solve the Issue.


Solution

  • As @Notus_Panda pointed out the problem was the content of the strings i wanted to write. The Strings started with an equal sign. I could fix it by editing it to "'=".Thanks!