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)
 
The code works fine for me.

Are you sure you 'Ref Sheet' is right?

Or perhaps the workbook you are trying to copy the worksheet into is protected.
 
Upvote 0

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Hi Noire,

Thanks a lot for your help. Unfortunately the code still doesn't work on my end. I confirmed the Ref Sheet name and verified that there are no protections on either workbook but still no luck. I'll keep poking at it and post a reply if I can figure out why it isn't working.

Allen

This is how my code stands for now

'
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
@rsxchin- still no luck, getting the same "Copy method of Worksheet class failed" error


<!-- / message -->
 
Upvote 0
Are there any chart sheets in either workbook?

That's about the last thing I can think of.

I can't even recreate the error you are getting.:eek:
 
Upvote 0

Forum statistics

Threads
1,223,277
Messages
6,171,153
Members
452,383
Latest member
woodsfordg

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