1

I have a problem in VBA. I created a script to sum up every monthly sum (lastrow of the table; every third column should be added) to a yearly sum. For that I create a string with the sum function in it. Then I want to paste the string into a cell under the monthly table.

Whenever I press the button

error 1004

pops up. I tried my best to solve it myself, but I couldn't fix it. If I pass the string to a msgbox it works fine and I get the =sum(...) displayed on my screen.

'Defintions
Dim lastCol As Long
Dim lastRow As Long
Dim i As Integer
Dim k As Integer
Dim x As Integer
Dim l As String
Dim Squantitys As String
Dim year As String
Dim startYear As Integer
Dim str As String
startYear = 8

'Get Dimensions of results
lastRow = Sheets("results").Cells(Rows.Count, 1).End(xlUp).Row
lastCol = Sheets("results").Cells(1, Columns.Count).End(xlToLeft).Column

'Get the new table ready
Sheets("results").Cells(lastRow + 7, 1).Value = "year"
Sheets("results").Cells(lastRow + 7, 2).Value = "total Quantitys"

'add total Quantitys
Squantitys = "=sum("

For i = 2 To lastCol Step 36

     If (lastCol - i) >= 36 Then

          For k = i To i + 33 Step 3

               l = Split(Sheets("results").Cells(lastRow, k).Address, "$")(1)

               Squantitys = Squantitys & l & lastRow & ";"

               x = k + 3

          Next

          l = Split(Sheets("results").Cells(lastRow, x).Address, "$")(1)

          Squantitys = Squantitys & l & lastRow & ")"

     Else

          For k = i To lastCol - 3 Step 3

               l = Split(Sheets("results").Cells(lastRow, k).Address, "$")(1)

               Squantitys = Squantitys & l & lastRow & ";"

               x = k + 3

          Next

          l = Split(Sheets("results").Cells(lastRow, x).Address, "$")(1)

          Squantitys = Squantitys & l & lastRow & ")"

     End If



     'output

     year = Sheets("results").Cells(1, i).Value

     Sheets("results").Cells(lastRow + startYear, 1).Value = year

     'MsgBox Squantitys --- this works fine
     Sheets("results").Cells(lastRow + startYear, 2).Value = Squantitys

     'reset for next loop

     startYear = startYear + 1
     Squantitys = "=sum("

Next

1 Answer 1

2
Squantitys = Squantitys & l & lastRow & ";"

here change ";" to ",". In VBA accepts only US format, not local

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot :) That solved it!!! Such a dumb mistake took hours for me

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.