VBA, Defining the current workbook as a variable

aallaman

New Member
Joined
Dec 4, 2009
Messages
44
Hi All,

I am writing a macro that starts in one workbook (who's file name will change weekly) and will also access a reference sheet on the network. The goal is to copy the reference sheet into the first workbook.

In order to do this I'm using the following code, but I am not getting past the Set CurMP line. Am I defining that variable incorrectly?

Thanks for your help!!!

Allen

Dim CurMP As Workbook
Set CurMP = Workbooks(ThisWorkbook)
Workbooks.Open Filename:= _
"G:\Ad Services\Analytics\FY11 Campaigns\FY11 Windows\Allen's Stuff\BigBrother\BigBrotherRefSheet.xlsx"
Sheets("Ref Sheet").Copy After:=Workbooks(CurMP).Sheets(1)
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Maybe this:

Set CurMP = Workbooks("ThisWorkbook")

Hi All,

I am writing a macro that starts in one workbook (who's file name will change weekly) and will also access a reference sheet on the network. The goal is to copy the reference sheet into the first workbook.

In order to do this I'm using the following code, but I am not getting past the Set CurMP line. Am I defining that variable incorrectly?

Thanks for your help!!!

Allen

Dim CurMP As Workbook
Set CurMP = Workbooks(ThisWorkbook)
Workbooks.Open Filename:= _
"G:\Ad Services\Analytics\FY11 Campaigns\FY11 Windows\Allen's Stuff\BigBrother\BigBrotherRefSheet.xlsx"
Sheets("Ref Sheet").Copy After:=Workbooks(CurMP).Sheets(1)
 
Upvote 0
If I leave the quotation marks out I get a "Type Mismatch" error

If I put them in then I get a "Subscript out of range" error
 
Upvote 0
Try this.
Code:
Set CurMP = ThisWorkbook
 
Upvote 0
Thanks everyone for your help!

@Norie - That gets me through setting the variable

Now I am getting a "Type Mismatch" error on this line.



Dim CurMP As Workbook
Set CurMP = ThisWorkbook
Workbooks.Open Filename:= _
"G:\Ad Services\Analytics\FY11 Campaigns\FY11 Windows\Allen's Stuff\BigBrother\BigBrotherRefSheet.xlsx"
Sheets("Ref Sheet").Copy After:=Workbooks(CurMP).Sheets(1)





I had this working the other day until I accidentally erased the macro. I cannot remember for the life of me how I did it.
 
Upvote 0
You don't need Workbooks - CurWB is a reference to ThisWorkbook.

It can just be used on it's own.

Something like this.
Code:
Set wbRef = Workbooks.Open (Filename:= _
"G:\Ad Services\Analytics\FY11 Campaigns\FY11 Windows\Allen's Stuff\BigBrother\BigBrotherRefSheet.xlsx")
 
wbRef.Sheets("Ref Sheet").Copy After:=CurMP.Sheets(1)
PS Hope you don't mind but I added a bit so you have a reference to the workbook getting opened too.:)
 
Upvote 0
What exact code do you have now?

Did you include this line?
Code:
Set wbRef = Workbooks.Open (Filename:= _
"G:\Ad Services\Analytics\FY11 Campaigns\FY11 Windows\Allen's Stuff\BigBrother\BigBrotherRefSheet.xlsx")
If you didn't that might explain the 'object required' error.

It creates a reference, wbRef, to the workbook you are opening.

While it's not strictly necessary to do that it could make things a lot easier.

For example if you want to close the workboook after you've copied the worksheet from it:
Code:
wbRef.Close
 
Upvote 0
Here is my exact code, I was missing the Set wbRef = statement.

Now I am back to getting a ""Copy method of Worksheet class failed" error

Dim CurMP As Workbook
Set CurMP = ThisWorkbook
Set wbRef = Workbooks.Open(Filename:= _
"G:\Ad Services\Analytics\FY11 Campaigns\FY11 Windows\Allen's Stuff\BigBrother\BigBrotherRefSheet.xlsx")
wbRef.Sheets("Ref Sheet").Copy After:=CurMP.Sheets(1)
 
Upvote 0

Forum statistics

Threads
1,224,517
Messages
6,179,242
Members
452,898
Latest member
Capolavoro009

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