selecting a workbook

kluitna

Board Regular
Joined
Mar 10, 2002
Messages
75
Here is my query. Is there a way to right in code that would tell excel to activate or select the workbook that the macro is located in.

example: I have a macro that opens another workbook and copies several cells from that workbook. Then I need to paste those copied cells into a the workbook that the macro was written in or associated with. I know I could do this buy using the file name of the
destination sheet, but that changes regularly. but the same macro will always be associated with a given workbook no matter what the workbook name. If anyone out here has any idea if this is possible I would apreciate the help.
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Yes, in your macro, before you open any other workbooks, just set a variable.

Dim currentworkbook as string

currentworkbook=activeworkbook.name

put that in before you open any other workbooks, then later you can refer to the original workbook as "currentworkbook"
 
Upvote 0
As an Alternative to RobFo0s help....

In your code Use the ThisWorkbook Object qualifyer to reference the workbook that the code is currently from.

Eg instead of

Workbook(whatever).dosomething

use

Thisworkbook.dosomething
 
Upvote 0

Forum statistics

Threads
1,213,506
Messages
6,114,024
Members
448,543
Latest member
MartinLarkin

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