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

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
The error means that it isn't finding something - probably a sheet. Workbooks("Actual_Participation_02_2011.xls").Sheets(1) must exist so the problem must be with Workbooks("Book1Template.xlsx").Sheets("Graphings")
 
Upvote 0
Not sure should find it as both the workbooks are open.....What i am trying to do is open the two workbooks and copy the range from Actual_Participation_02_2011.xls range A2:A1000 and then paste it into Book1Template.xlsx in the sheet graphing into B3:B10001 I tried writing VBA to copy and paste seperately but i couldn't get to work so i tried the line above which also doesn't work.
 
Upvote 0
Does this work?

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").Copy Destination:=Workbooks("Book1Template.xlsx").Sheets("Graphings").Range("B3")

End Sub
 
Upvote 0
Well, I'm sorry but a sheet (probably Graphings) must be named wrongly (Excel can't find it).
 
Upvote 0
Then you can't have a sheet called Graphings, I don't think. Just to test:
Code:
Sub AverageGraph()
Dim wbk1 as workbook, wbk2 as workbook
set wbk1 = Application.Workbooks.Open("C:\Documents and Settings\SeymourJ\Desktop\Book1Template.xlsx")
set wbk2 = Application.Workbooks.Open("C:\Documents and Settings\SeymourJ\Desktop\Actual_Participation_02_2011.xls")
wbk2.Sheets(1).Range("A2:A1000").Copy Destination:=wbk1.Sheets("Graphings").Range("B3")
End Sub
 
Upvote 0
Hi

I have figurerd it out book1template is wrong its book2 not book1.

Thank you for all your help....you have been very helpful :eeek:
 
Upvote 0

Forum statistics

Threads
1,224,531
Messages
6,179,379
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