Dirkelton
New Member
- Joined
- Feb 16, 2011
- Messages
- 7
I'm currently trying to learn VBA via a step by step book. I'm getting on OK but have hit a brick wall with the following code to auto fill a formula to a range.
Sub FillFormulas()
Dim myOuter As Range
Dim myInner As Range
Dim myFormula As String
Set myOuter = Cells.Find("price").CurrentRegion
Set myInner = Intersect(myOuter, myOuter.Offset(2, 1))
Cells.Find("Discount").CurrentRegion.CreateNames True
myFormula = myOuter.Range("b2").Address(True, False, xlR1C1, False, myInner)
myFormula = "=" & myFormula & "*"
myFormula = myFormula & _
myOuter.Range("A3").Address(False, True, xlR1C1, False, myInner)
myFormula = myFormula & " * (1 - Discount)"
myInner.FormulaR1C1 = myFormula
End Sub
I've input as advised in the book but keep getting Run Time Error '91': Object Variable or With block variable not set.
Everything works ok in the Immediate Window but just doesn't run as a macro.
Can anyone help please
Sub FillFormulas()
Dim myOuter As Range
Dim myInner As Range
Dim myFormula As String
Set myOuter = Cells.Find("price").CurrentRegion
Set myInner = Intersect(myOuter, myOuter.Offset(2, 1))
Cells.Find("Discount").CurrentRegion.CreateNames True
myFormula = myOuter.Range("b2").Address(True, False, xlR1C1, False, myInner)
myFormula = "=" & myFormula & "*"
myFormula = myFormula & _
myOuter.Range("A3").Address(False, True, xlR1C1, False, myInner)
myFormula = myFormula & " * (1 - Discount)"
myInner.FormulaR1C1 = myFormula
End Sub
I've input as advised in the book but keep getting Run Time Error '91': Object Variable or With block variable not set.
Everything works ok in the Immediate Window but just doesn't run as a macro.
Can anyone help please