I am trying to sum columns with data after a change is made to the worksheet. The columns that contain data have text in Row 1.
The first time I test it, I get
Run-time error '28': Out of stack space
The second time I test it, I get
Run-time error '-2147417848 (80010108)': Method 'Value' of object 'Range' failed, and then Excel crashes.
Sub Worksheet_Change(ByVal Target As Range)
Dim LastCol As Integer
Dim NumProjects As Integer
Dim i As Integer
With ActiveSheet
LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
End With
NumProjects = LastCol - 5
For i = 1 To NumProjects
Range("E44").Offset(0, i).Value = WorksheetFunction.Sum(Range("E2").Offset(0, i), Range("E43").Offset(0, i))
Next i
End Sub
NumProjects and LastCol As long.Forloop is doing: F44 = F2 + F43; G44 = G2 + G43; ... up to NumProjects. If that's what you intended, having non-numeric data in rows 2 or 43 could cause a problem. As an aside, instead of callingWorksheetFunction.Sumyou could just add the two numbers:.Value = Range("E2").Offset(0, i) + Range("E43").Offset(0, i)Range("E2:E43").Offset(0, i).Targetrange. Do you want this to run every single time any cell is changed?