|
Question : Help with syntax of a vba range definition / sum
|
|
I am really having trouble getting the syntax of ranges straight for some reason. The sub below should be taking the total for 27 cells and putting that total in a cell (as a number, not as a formula). When I run the code, I get error "run-time error '1004': Method 'Range' of object '_Worksheet' failed" on the line with the sum statement. Any clue what the issue is?
FWIW, msgbox rng.count results in "27" which is the correct count.
Sub TotalKP()
Dim ws, ws1 As Worksheet Dim r, c, r1, c1 As Integer 'w is the number of columns to count out for totals Dim w As Integer Dim rng As Range Dim tot As Long
Set ws1 = Worksheets("Main")
lastrow1 = ws1.UsedRange.Row + ws1.UsedRange.Rows.count - 1&
r1 = 5 c1 = 1 w = 26
While r1 < lastrow1 + 1 If ws1.Cells(r1, c1 + 2).Value <> "" Then Set rng = ws1.Range(Cells(r1, c1 + 13), Cells(r1, (c1 + 13) + w)) MsgBox rng.count ws1.Cells(r1, c1 + 12).Value = WorksheetFunction.Sum(Range("rng")) End If r1 = r1 + 1 Wend
End Sub
|
|
Answer : Help with syntax of a vba range definition / sum
|
|
change this >> ws1.Cells(r1, c1 + 12).Value = WorksheetFunction.Sum(Range("rng"))
to this
ws1.Cells(r1, c1 + 12).Value = WorksheetFunction.Sum(rng)
Tom
|
|
|
|