Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.excel.programming
|
|||
|
|||
Can I call a macro from a specific book
I have a number of workbooks open all with a macro called 'MyMacro' in them
2 Questions: 1. When there are a few open, how can I run the macro from a particular book (I want something like: Call Workbooks("Book1.xls").MyMacro) 2. Also... How can I run the MyMacro macro from the ActiveWorkbook? Thanks M |
#2
Posted to microsoft.public.excel.programming
|
|||
|
|||
Can I call a macro from a specific book
Use Application.Run: Code: -------------------- Application.Run "'Book1.xls'!MyMacro" -------------------- Michelle;723207 Wrote: I have a number of workbooks open all with a macro called 'MyMacro' in them 2 Questions: 1. When there are a few open, how can I run the macro from a particular book (I want something like: Call Workbooks("Book1.xls").MyMacro) 2. Also... How can I run the MyMacro macro from the ActiveWorkbook? Thanks M -- aflatoon Regards, A. ------------------------------------------------------------------------ aflatoon's Profile: http://www.thecodecage.com/forumz/member.php?u=1501 View this thread: http://www.thecodecage.com/forumz/sh...d.php?t=202552 http://www.thecodecage.com/forumz |
#3
Posted to microsoft.public.excel.programming
|
|||
|
|||
Can I call a macro from a specific book
First, I think that this is a very bad idea (using the same name). I've seen
excel run the macro it thinks it should run instead of the one I want. But in my light testing today, this worked ok for me in xl2003 under winXP Home. (I'm not sure what version I've see the problem with.) #1. You can use something like this: Option Explicit Sub testme() Dim OtherWkbk As Workbook Set OtherWkbk = Workbooks("book1.xls") Application.Run "'" & OtherWkbk.Name & "'!mymacro" End Sub #2. If you really mean the activeworkbook's project -- not the workbook's project that's running the code: Option Explicit Sub testme() Dim OtherWkbk As Workbook Set OtherWkbk = ActiveWorkbook Application.Run "'" & OtherWkbk.Name & "'!mymacro" End Sub If you meant that you wanted to have a procedure call another procedure in the same project: Option Explicit Sub testme() Call myMacro 'or even Dim OtherWkbk As Workbook Set OtherWkbk = ThisWorkbook Application.Run "'" & OtherWkbk.Name & "'!mymacro" End Sub Again, I've seen some very bad problems when I've tried to do this in real life. I'd spend some time making nice names. In fact, if those macros are all the same (and work against the active sheet or active workbook, then I think I'd create an addin that contained the code. And remove all that code from the individual workbooks. It would make life lots simpler (only one file to update). And I could just open that addin when I needed to run any of those macros. Michelle wrote: I have a number of workbooks open all with a macro called 'MyMacro' in them 2 Questions: 1. When there are a few open, how can I run the macro from a particular book (I want something like: Call Workbooks("Book1.xls").MyMacro) 2. Also... How can I run the MyMacro macro from the ActiveWorkbook? Thanks M -- Dave Peterson |
#4
Posted to microsoft.public.excel.programming
|
|||
|
|||
Can I call a macro from a specific book
Thanks - good advice. I may have to ignore it, but it's great that you've
made me think it through again. M "Dave Peterson" wrote in message ... First, I think that this is a very bad idea (using the same name). I've seen excel run the macro it thinks it should run instead of the one I want. But in my light testing today, this worked ok for me in xl2003 under winXP Home. (I'm not sure what version I've see the problem with.) #1. You can use something like this: Option Explicit Sub testme() Dim OtherWkbk As Workbook Set OtherWkbk = Workbooks("book1.xls") Application.Run "'" & OtherWkbk.Name & "'!mymacro" End Sub #2. If you really mean the activeworkbook's project -- not the workbook's project that's running the code: Option Explicit Sub testme() Dim OtherWkbk As Workbook Set OtherWkbk = ActiveWorkbook Application.Run "'" & OtherWkbk.Name & "'!mymacro" End Sub If you meant that you wanted to have a procedure call another procedure in the same project: Option Explicit Sub testme() Call myMacro 'or even Dim OtherWkbk As Workbook Set OtherWkbk = ThisWorkbook Application.Run "'" & OtherWkbk.Name & "'!mymacro" End Sub Again, I've seen some very bad problems when I've tried to do this in real life. I'd spend some time making nice names. In fact, if those macros are all the same (and work against the active sheet or active workbook, then I think I'd create an addin that contained the code. And remove all that code from the individual workbooks. It would make life lots simpler (only one file to update). And I could just open that addin when I needed to run any of those macros. Michelle wrote: I have a number of workbooks open all with a macro called 'MyMacro' in them 2 Questions: 1. When there are a few open, how can I run the macro from a particular book (I want something like: Call Workbooks("Book1.xls").MyMacro) 2. Also... How can I run the MyMacro macro from the ActiveWorkbook? Thanks M -- Dave Peterson |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
Bug: Call Sub in other book | Excel Programming | |||
how do I call specific worksheet | Excel Programming | |||
Can you call functions between sheets in the same book in excel? | Excel Worksheet Functions | |||
macro to copy specific rows to a new book | Excel Programming | |||
Macro to automaticaly save a book under an specific name | Excel Programming |