PC Review


Reply
Thread Tools Rate Thread

Application.Run for every open workbooks (one by one)

 
 
mxp44@yahoo.com
Guest
Posts: n/a
 
      21st Oct 2006
Hi guys,

One question:

Do you know how to convert the macro:

Sub UglyMacro ()
Application.Run
"FirstFile.xls!SameMacroButStoredInThisSpecificWorkbook"
ActiveWindow.ActivateNext
Application.Run
"SecondFile.xls!SameMacroButStoredInThisSpecificWorkbook"
ActiveWindow.ActivateNext
Application.Run
"ThirdFile.xls!SameMacroButStoredInThisSpecificWorkbook"
ActiveWindow.ActivateNext
End Sub

into macro that will go like:

Sub DraftNiceMacro ()
For each wbk in Application.Workbooks
Application.Run "wbk.SameMacroButStoredInThisSpecificWorkbook"
Next wbk
End Sub

This one is not working for some strange reason...

Thanks for your help,
Mark

 
Reply With Quote
 
 
 
 
Dave Peterson
Guest
Posts: n/a
 
      21st Oct 2006
Notice your syntax:
"FirstFile.xls!SameMacroButStoredInThisSpecificWorkbook"

You have an exclamation mark after the workbook name and you use the workbook
name.

Application.Run "'" & wbk.name & "'!SameMacroButStoredInThisSpecificWorkbook"

The apostrophes are sometimes required--depending on the name of the file. But
they never hurt if you include them.

(E-Mail Removed) wrote:
>
> Hi guys,
>
> One question:
>
> Do you know how to convert the macro:
>
> Sub UglyMacro ()
> Application.Run
> "FirstFile.xls!SameMacroButStoredInThisSpecificWorkbook"
> ActiveWindow.ActivateNext
> Application.Run
> "SecondFile.xls!SameMacroButStoredInThisSpecificWorkbook"
> ActiveWindow.ActivateNext
> Application.Run
> "ThirdFile.xls!SameMacroButStoredInThisSpecificWorkbook"
> ActiveWindow.ActivateNext
> End Sub
>
> into macro that will go like:
>
> Sub DraftNiceMacro ()
> For each wbk in Application.Workbooks
> Application.Run "wbk.SameMacroButStoredInThisSpecificWorkbook"
> Next wbk
> End Sub
>
> This one is not working for some strange reason...
>
> Thanks for your help,
> Mark


--

Dave Peterson
 
Reply With Quote
 
John Coleman
Guest
Posts: n/a
 
      21st Oct 2006
If the same macro is used in several workbooks maybe you can move it
into an add-in. Just an idea.

Maybe you can try

For each wb in Application.Workbooks
Application.Run wb.Name & ".xls!" & "SameMacro"
Next wb

I didn't test it but I hope it helps

-John Coleman

(E-Mail Removed) wrote:
> Hi guys,
>
> One question:
>
> Do you know how to convert the macro:
>
> Sub UglyMacro ()
> Application.Run
> "FirstFile.xls!SameMacroButStoredInThisSpecificWorkbook"
> ActiveWindow.ActivateNext
> Application.Run
> "SecondFile.xls!SameMacroButStoredInThisSpecificWorkbook"
> ActiveWindow.ActivateNext
> Application.Run
> "ThirdFile.xls!SameMacroButStoredInThisSpecificWorkbook"
> ActiveWindow.ActivateNext
> End Sub
>
> into macro that will go like:
>
> Sub DraftNiceMacro ()
> For each wbk in Application.Workbooks
> Application.Run "wbk.SameMacroButStoredInThisSpecificWorkbook"
> Next wbk
> End Sub
>
> This one is not working for some strange reason...
>
> Thanks for your help,
> Mark


 
Reply With Quote
 
mxp44@yahoo.com
Guest
Posts: n/a
 
      21st Oct 2006
Hi Dave,
Hi John,
Thanks a lot for your answers!

Just for feedback:
The code provided by Dave was almost perfect, I just needed to add
"wbk.Activate", as below:
-------------
For Each wbk In Application.Workbooks
wbk.Activate
Application.Run "'" & wbk.Name & "'!SameMacro"
Next i
------------

On the other hand, I was unable to run your code, John. Suppose I'm
missing something...

Cheers,
Mark

 
Reply With Quote
 
Dave Peterson
Guest
Posts: n/a
 
      21st Oct 2006
In general, you don't need to activate the workbook to run the macro.

But depending on what your code does and what workbook/worksheet should be
affected, you may even want to activate/select a specific sheet, too.

(E-Mail Removed) wrote:
>
> Hi Dave,
> Hi John,
> Thanks a lot for your answers!
>
> Just for feedback:
> The code provided by Dave was almost perfect, I just needed to add
> "wbk.Activate", as below:
> -------------
> For Each wbk In Application.Workbooks
> wbk.Activate
> Application.Run "'" & wbk.Name & "'!SameMacro"
> Next i
> ------------
>
> On the other hand, I was unable to run your code, John. Suppose I'm
> missing something...
>
> Cheers,
> Mark


--

Dave Peterson
 
Reply With Quote
 
Tom Ogilvy
Guest
Posts: n/a
 
      21st Oct 2006
wb.name would include "xls", so you don't need to add that.

--
Regards,
Tom Ogilvy


"John Coleman" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> If the same macro is used in several workbooks maybe you can move it
> into an add-in. Just an idea.
>
> Maybe you can try
>
> For each wb in Application.Workbooks
> Application.Run wb.Name & ".xls!" & "SameMacro"
> Next wb
>
> I didn't test it but I hope it helps
>
> -John Coleman
>
> (E-Mail Removed) wrote:
>> Hi guys,
>>
>> One question:
>>
>> Do you know how to convert the macro:
>>
>> Sub UglyMacro ()
>> Application.Run
>> "FirstFile.xls!SameMacroButStoredInThisSpecificWorkbook"
>> ActiveWindow.ActivateNext
>> Application.Run
>> "SecondFile.xls!SameMacroButStoredInThisSpecificWorkbook"
>> ActiveWindow.ActivateNext
>> Application.Run
>> "ThirdFile.xls!SameMacroButStoredInThisSpecificWorkbook"
>> ActiveWindow.ActivateNext
>> End Sub
>>
>> into macro that will go like:
>>
>> Sub DraftNiceMacro ()
>> For each wbk in Application.Workbooks
>> Application.Run "wbk.SameMacroButStoredInThisSpecificWorkbook"
>> Next wbk
>> End Sub
>>
>> This one is not working for some strange reason...
>>
>> Thanks for your help,
>> Mark

>



 
Reply With Quote
 
John Coleman
Guest
Posts: n/a
 
      21st Oct 2006
Thanks Tom
I see what happened. I wasn't sure about that point off the top of my
head so before my post I opened up Excel and wrote a quick sub to run
MsgBox ActiveWorkbook.Name and saw "Book 1" displayed - but I hadn't
*saved* it yet and the .xls is only added upon the first save.
Thanks again
-John Coleman

Tom Ogilvy wrote:
> wb.name would include "xls", so you don't need to add that.
>
> --
> Regards,
> Tom Ogilvy
>
>
> "John Coleman" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > If the same macro is used in several workbooks maybe you can move it
> > into an add-in. Just an idea.
> >
> > Maybe you can try
> >
> > For each wb in Application.Workbooks
> > Application.Run wb.Name & ".xls!" & "SameMacro"
> > Next wb
> >
> > I didn't test it but I hope it helps
> >
> > -John Coleman
> >
> > (E-Mail Removed) wrote:
> >> Hi guys,
> >>
> >> One question:
> >>
> >> Do you know how to convert the macro:
> >>
> >> Sub UglyMacro ()
> >> Application.Run
> >> "FirstFile.xls!SameMacroButStoredInThisSpecificWorkbook"
> >> ActiveWindow.ActivateNext
> >> Application.Run
> >> "SecondFile.xls!SameMacroButStoredInThisSpecificWorkbook"
> >> ActiveWindow.ActivateNext
> >> Application.Run
> >> "ThirdFile.xls!SameMacroButStoredInThisSpecificWorkbook"
> >> ActiveWindow.ActivateNext
> >> End Sub
> >>
> >> into macro that will go like:
> >>
> >> Sub DraftNiceMacro ()
> >> For each wbk in Application.Workbooks
> >> Application.Run "wbk.SameMacroButStoredInThisSpecificWorkbook"
> >> Next wbk
> >> End Sub
> >>
> >> This one is not working for some strange reason...
> >>
> >> Thanks for your help,
> >> Mark

> >


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
VBA Workbooks.Open() / method 'open' of object 'workbooks' failed Luc Dindeman Microsoft Excel Crashes 0 25th Mar 2009 12:03 PM
Excel COM application hangs on Workbooks.Open =?Utf-8?B?b3dkaXRocw==?= Microsoft Excel Programming 1 18th Jan 2007 09:27 AM
Problem with Excel.Application.Workbooks.Open() Arunabh Microsoft VB .NET 1 16th Feb 2006 05:52 PM
Using workbooks.open after new instance of excel application George J Microsoft Excel Programming 3 16th Sep 2004 02:00 PM
Excel 2003 Workbooks.Open with CorruptLoad=xlRepairFile fails on Excel 5.0/95 file due to Chart, with Error 1004 Method 'Open' of object 'Workbooks' failed Frank Jones Microsoft Excel Programming 2 15th Jun 2004 03:21 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:08 PM.