Object variable not set???

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 :banghead:
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Unfortunately your code is very obscure - what are the ranges "price" and "discount". Why would you be intersecting them? What is the formula you are adding? An error could occur at any and all of these lines.

However, you can learn a lot by debugging your code:
http://krgreenlee.blogspot.com/2006/04/programming-excel-vba-debugging-for.html

I would suggest you step through line by line an check what each line is doing. Actually, I suggest you toss this section of the book and move on - it's a strange bit of code to be using for educational purposes.
 
Upvote 0

Forum statistics

Threads
1,224,597
Messages
6,179,813
Members
452,945
Latest member
Bib195

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top