'Type mismatch with Offset function Excel VBA
I'm new to VBA macros, the function has to read a formula, write the result to resultCell and go down to have other values in the formula. Everything was fine, but it only counted the first values (BJ4-AV4), so that it counts further BJ5, BJ6, BJ7... I used offset(i,0) so it should go down. I already used offset to write the result, but when I entered it to go from cell to cell in the formula, it broke. I hope the code helps make sense of the nonsense I wrote
Sub avg()
Dim firstCell, resultCell As Range
Dim i As Integer
Set firstCell = Worksheets(1).Range("BJ4")
Set resultCell = Worksheets(1).Range("CA4")
For i = 0 To 502
' go to 12 left
If firstCell.Offset(i, -12).Value > 0 Then
resultCell.Offset(i, 0).Value = (firstCell.Offset(i, 0).Value - firstCell.Offset(i, -12).Value) / 6
ElseIf firstCell.Offset(i, -10).Value > 0 Then
' if previous is null go to 10 left
resultCell.Offset(i, 0).Value = (firstCell.Offset(i, 0).Value - firstCell.Offset(i, -10).Value) / 5
ElseIf firstCell.Offset(i, -14).Value > 0 Then
' if previous is null go to 14 left
resultCell.Offset(i, 0).Value = (firstCell.Offset(i, 0).Value - firstCell.Offset(i, -14).Value) / 7
End If
Next i
End Sub
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
