I am trying to add a dynamic formula to a cell using VBA. I have seen a couple of post on it but I cannot figure out what I am doing wrong. I am getting a run-time error '1004' for "Method 'Range' of object'_Worksheet' failed". What am I doing wrong?
Sub AddFormulas()
Dim LastNumberRow As Integer
Set countBase = Sheet7.Range("CU2")
colCount = Sheet7.Range(countBase, countBase.End(xlToRight)).Columns.Count
Dim startCount As Integer
startCount = 98
For i = 1 To colCount
If IsNumeric(Cells(2, startCount + i)) Then
Sheet7.Range(3, i).Formula = "=Sheet6!" & Cells(3, startCount + i).Address & "*" & "Sheet7!" & Cells(3, colCount + startCount).Address
Else
'Do some stuff
End If
Next i
End Sub
I have added the proposed changes from the comments below but not I get a file explorer popup window. I have altered my code with the following changes:
If IsNumeric(Sheet7.Cells(2, startCount + i)) Then
Set bSum = Sheet7.Cells(3, colCount + startCount)
Set bSpr = Sheet6.Cells(3, startCount + i)
Sheet7.Cells(3, i).Formula = "=Sheet6!" & bSpr.Address() & "*" & "Sheet7!" & bSpr.Address()
Sheet7.Range(3, i).Formulashould beSheet7.Cells(3, i).FormulaSheet7.Cells(2, startCount + i)orSheet6.Cells(2, startCount + i)Cells(3, startCount + i)andCells(3, colCount + startCount)to be on? You should add the worksheet name/index before that too, like forSheet7.Range().