Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 204
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 420
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 204
Default 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
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Bug: Call Sub in other book jotor14 Excel Programming 4 June 26th 06 07:38 PM
how do I call specific worksheet vbidiot[_10_] Excel Programming 4 March 4th 06 09:38 PM
Can you call functions between sheets in the same book in excel? Arenlor Excel Worksheet Functions 3 January 7th 06 03:21 AM
macro to copy specific rows to a new book Karen W Excel Programming 1 February 24th 05 06:46 PM
Macro to automaticaly save a book under an specific name Marissa[_2_] Excel Programming 1 August 5th 04 02:08 AM


All times are GMT +1. The time now is 04:58 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"