Compile Error

  • Thread starter Thread starter Legacy 93538
  • Start date Start date
L

Legacy 93538

Guest
Hi

I have written a macro in VBA which should open two documents and copies data from A2:A1000 to B3:B2000

Code:
 Option Explicit
Sub AverageGraph()
 
Application.Workbooks.Open ("C:\Documents and Settings\SeymourJ\Desktop\Book1Template.xlsx")
 
Application.Workbooks.Open ("C:\Documents and Settings\SeymourJ\Desktop\Actual_Participation_02_2011.xls")
 
Actual_Participation_02_2011.Sheet(1).Range("A2:A1000").Value = Book1Template.xlsx.Sheet("Graphings").Range("B3").Value

End Sub

However every time i run it i get a Compile Error saying Variable not defined. Can anyone tell me why i am getting it?

Thanks

JessicaSeymour
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Try

Rich (BB code):
Sub AverageGraph()
 
Application.Workbooks.Open "C:\Documents and Settings\SeymourJ\Desktop\Book1Template.xlsx"
 
Application.Workbooks.Open "C:\Documents and Settings\SeymourJ\Desktop\Actual_Participation_02_2011.xls"
 
Workbooks("Actual_Participation_02_2011.xls").Sheets(1).Range("A2:A1000").Value = Workbooks("Book1Template.xlsx").Sheets("Graphings").Range("B3").Value

End Sub
 
Upvote 0
You have two undefined variables in this line:

Rich (BB code):
Actual_Participation_02_2011.Sheet(1).Range("A2:A1000").Value = Book1Template.xlsx.Sheet("Graphings").Range("B3").Value

You need to either set variables to be these values, or refer to the workbooks using the Workbooks() object:

Rich (BB code):
Workbooks("Actual_Participation_02_2011.xlsx").Sheet(1).Range("A2:A1000").Value = Workbooks("Book1Template.xlsx").Sheet("Graphings").Range("B3").Value
 
Upvote 0
Hi

Thanks for replying. I ahve tried both ways and it came up with another error saying "Object Doesn't supportthis property or method"
 
Upvote 0
No i forgot....i ahve not changed them and i ran it again andi got another error saying subscript out of range
 
Upvote 0
Check carefully the spelling of the Graphings sheet - check for leading or trailing spaces.
 
Upvote 0
Ok...it hightlights this line

Code:
 Workbooks("Actual_Participation_02_2011.xlsx").Sheets(1).Range("A2:A1000").Value = Workbooks("Book1Template.xlsx").Sheets("Graphing").Range("B3").Value
 
Upvote 0

Forum statistics

Threads
1,224,527
Messages
6,179,331
Members
452,907
Latest member
Roland Deschain

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