Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

selecting window with macro

Status
Not open for further replies.

Luis939

MIS
Feb 28, 2003
453
US
I'm pretty sure I saw something like this in the help file but I forgot where, basically, I'm transfering data from one workbook to another, but i dont want it so that i have to refer to the specific workbook with a specific name. I wnat to have a macro in the personal workbook so i can use it with any workbook, but lets say i run the macro from one workbook (book1) taking its data and putting into another workbook (book2), instead of sayin

Windows("Book2").Activate

cant i make it so that it says go to the workbook where the macro was run, because ill have many different workbooks with differetn names, and i dont want to have to worry about something like that, thanks
 
Hi 4335,

I'm not sure I understand your question completely.

Your macro can only be run from one workbook, the active workbook and you can get its name from ActiveWorkbook.Name

If you want to reference another workbook you must either hard code it or, possibly, deduce it by looking at all the open workbooks with some code like ..

Code:
Dim B as Workbook
For Each B in Application.Workbooks
    ' Pick up the name here and do what you will
Next

Also note that for many actions it is not necessary to activate a workbook, just to reference a range in it.

Enjoy,
Tony
 
well basically i just want to refer to the workbook that ran the macro, instead of specifying its actual name, i wanted to know if there was something that said activate the workbook (whichever it may be) that was the one that started that macro, or in other words, the workbook that was active at the time that i chose the macro to run
 
well basically i just want to refer to the workbook that ran the macro, instead of specifying its actual name, i wanted to know if there was something that said activate the workbook (whichever it may be) that was the one that started that macro, or in other words, the workbook that was active at the time that i chose the macro to run
 
In case I wasn't entirely clear, I don't think there's any way to pick it after you've activated another workbook, but you can save it yourself at the start of the macro and then refer to it later.

At start of macro ...

Code:
Dim strBook1 as String
strBook1 = ActiveWorkbook.Name

later on ...

Code:
Workbooks(strBook1).Activate

Enjoy,
Tony
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top