Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 13
Default Running Macro/VBA in multiple worksheets.

I am trying to run a Macro (which works perfectly) in All Worksheets of a
particular Workbook. I currently have seven Worksheets in the Workbook and
have to select each Worksheet and manually run the Macro. I will eventually
have 50 (maybe more) Worksheets where the Macro will need to be run. I have
used the Private Sub code in VBA but can only get it to execute the Macro
in the active Worksheet when the Workbook opens.

Is there code that could/would run the Macro in All the Sheets? Sheet1,
Sheet2, Sheet3, Sheet4

Any help would be greatly appreciated.

--
SHD
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default Running Macro/VBA in multiple worksheets.

For Each sh In ActiveWorkbook.Worksheets
Call MyMacro
Next sh

You might need to pass the sh object to the macro so that it can work on
that, like this

For Each sh In ActiveWorkbook.Worksheets
Call MyMacro(sh)
Next sh

Sub MyMacro(pSheet as worksheet)
With pSheet
'...
End With
End Sub

--
HTH

Bob Phillips

(remove nothere from email address if mailing direct)

"YellowBird" wrote in message
...
I am trying to run a Macro (which works perfectly) in "All" Worksheets of

a
particular Workbook. I currently have seven Worksheets in the Workbook

and
have to select each Worksheet and manually run the Macro. I will

eventually
have 50 (maybe more) Worksheets where the Macro will need to be run. I

have
used the "Private Sub" code in VBA but can only get it to execute the

Macro
in the active Worksheet when the Workbook opens.

Is there code that could/would run the Macro in "All" the Sheets? Sheet1,
Sheet2, Sheet3, Sheet4.

Any help would be greatly appreciated.

--
SHD



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 13
Default Running Macro/VBA in multiple worksheets.

Bob,

I'm not sure if you reviewed the reply I sent to Gary but basically I had
the same result with your suggestion. The Marco ran 7 times which I assume
was 1 for each Worksheet in the Workbook. It doesn't seem to be switching to
the other Worksheets.

Any other thoughts/suggestions are welcome.

Thanks.
--
SHD


"Bob Phillips" wrote:

For Each sh In ActiveWorkbook.Worksheets
Call MyMacro
Next sh

You might need to pass the sh object to the macro so that it can work on
that, like this

For Each sh In ActiveWorkbook.Worksheets
Call MyMacro(sh)
Next sh

Sub MyMacro(pSheet as worksheet)
With pSheet
'...
End With
End Sub

--
HTH

Bob Phillips

(remove nothere from email address if mailing direct)

"YellowBird" wrote in message
...
I am trying to run a Macro (which works perfectly) in "All" Worksheets of

a
particular Workbook. I currently have seven Worksheets in the Workbook

and
have to select each Worksheet and manually run the Macro. I will

eventually
have 50 (maybe more) Worksheets where the Macro will need to be run. I

have
used the "Private Sub" code in VBA but can only get it to execute the

Macro
in the active Worksheet when the Workbook opens.

Is there code that could/would run the Macro in "All" the Sheets? Sheet1,
Sheet2, Sheet3, Sheet4.

Any help would be greatly appreciated.

--
SHD




  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default Running Macro/VBA in multiple worksheets.

It will run 7 time but you need to adjust your code for each individual run
to address the correct sheet. I showed the basic mechanism earlier, but
without seeing your macro, I cannot absolutely state what would need
adjusting.

There is no need to activate each sheet, you can work on them without doing
so.

--
HTH

Bob Phillips

(remove nothere from email address if mailing direct)

"YellowBird" wrote in message
...
Bob,

I'm not sure if you reviewed the reply I sent to Gary but basically I had
the same result with your suggestion. The Marco ran 7 times which I

assume
was 1 for each Worksheet in the Workbook. It doesn't seem to be switching

to
the other Worksheets.

Any other thoughts/suggestions are welcome.

Thanks.
--
SHD


"Bob Phillips" wrote:

For Each sh In ActiveWorkbook.Worksheets
Call MyMacro
Next sh

You might need to pass the sh object to the macro so that it can work on
that, like this

For Each sh In ActiveWorkbook.Worksheets
Call MyMacro(sh)
Next sh

Sub MyMacro(pSheet as worksheet)
With pSheet
'...
End With
End Sub

--
HTH

Bob Phillips

(remove nothere from email address if mailing direct)

"YellowBird" wrote in message
...
I am trying to run a Macro (which works perfectly) in "All" Worksheets

of
a
particular Workbook. I currently have seven Worksheets in the

Workbook
and
have to select each Worksheet and manually run the Macro. I will

eventually
have 50 (maybe more) Worksheets where the Macro will need to be run.

I
have
used the "Private Sub" code in VBA but can only get it to execute the

Macro
in the active Worksheet when the Workbook opens.

Is there code that could/would run the Macro in "All" the Sheets?

Sheet1,
Sheet2, Sheet3, Sheet4.

Any help would be greatly appreciated.

--
SHD






  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 13
Default Running Macro/VBA in multiple worksheets.

Bob,

Thank you for your response. While I know my way around Excel, I've just
started trying my "Luck" With Macros.

The following is the code for the Macro that I run. At this point, I can
make it run after I open the Workbook using the ALT F8 command but only in
the active Worksheet. I have to switch manually from Sheet to Sheet to
execute the Macro in all Sheets. I would like to call the Macro once and
have it run once in all Worksheets in the Workbook. The way I would like to
run the Macro is manually, one time, after the Workbook is opened and have it
run the Macro in each Worksheet in the Workbook.

' Update_Research_Data Macro
'
Range("A10").Select
Selection.EntireRow.Insert
Range("A9:F9").Select
Selection.Copy
Range("A10").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone,
SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Range("G9:U9").Select
Selection.Copy
Range("G10").Select
Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
Range("A9").Select
End Sub

Thanking you in advance for any help you can provide. It is greatly
appreciated.

--
SHD


"Bob Phillips" wrote:

It will run 7 time but you need to adjust your code for each individual run
to address the correct sheet. I showed the basic mechanism earlier, but
without seeing your macro, I cannot absolutely state what would need
adjusting.

There is no need to activate each sheet, you can work on them without doing
so.

--
HTH

Bob Phillips

(remove nothere from email address if mailing direct)

"YellowBird" wrote in message
...
Bob,

I'm not sure if you reviewed the reply I sent to Gary but basically I had
the same result with your suggestion. The Marco ran 7 times which I

assume
was 1 for each Worksheet in the Workbook. It doesn't seem to be switching

to
the other Worksheets.

Any other thoughts/suggestions are welcome.

Thanks.
--
SHD


"Bob Phillips" wrote:

For Each sh In ActiveWorkbook.Worksheets
Call MyMacro
Next sh

You might need to pass the sh object to the macro so that it can work on
that, like this

For Each sh In ActiveWorkbook.Worksheets
Call MyMacro(sh)
Next sh

Sub MyMacro(pSheet as worksheet)
With pSheet
'...
End With
End Sub

--
HTH

Bob Phillips

(remove nothere from email address if mailing direct)

"YellowBird" wrote in message
...
I am trying to run a Macro (which works perfectly) in "All" Worksheets

of
a
particular Workbook. I currently have seven Worksheets in the

Workbook
and
have to select each Worksheet and manually run the Macro. I will
eventually
have 50 (maybe more) Worksheets where the Macro will need to be run.

I
have
used the "Private Sub" code in VBA but can only get it to execute the
Macro
in the active Worksheet when the Workbook opens.

Is there code that could/would run the Macro in "All" the Sheets?

Sheet1,
Sheet2, Sheet3, Sheet4.

Any help would be greatly appreciated.

--
SHD








  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Running Macro/VBA in multiple worksheets.

Sub UpdateResearch()
' Update_Research_Data Macro
'
Dim sh as Worksheet
for each sh in Workbook.Worksheets
sh.Select
Range("A10").Select
Selection.EntireRow.Insert
Range("A9:F9").Select
Selection.Copy
Range("A10").Select
Selection.PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, _
SkipBlanks:=False, _
Transpose:=False
Application.CutCopyMode = False
Range("G9:U9").Select
Selection.Copy
Range("G10").Select
Selection.PasteSpecial Paste:=xlPasteFormulas, _
Operation:=xlNone, _
SkipBlanks:=False, _
Transpose:=False
Application.CutCopyMode = False
Range("A9").Select
Next Sh
end sub

or

Sub UpdateResearch1()
for each sh in Workbook.Worksheets
sh.Select
sh.Range("A9:F9").copy
sh.Range("A10").PasteSpecial Paste:=xlPasteValues
sh.Range("G9:U9").copy
sh.Range("G10").PasteSpecial Paste:=xlPasteFormulas
sh.Range("A9").Select
Next
End Sub

--
Regards,
Tom Ogilvy


"YellowBird" wrote in message
...
Bob,

Thank you for your response. While I know my way around Excel, I've just
started trying my "Luck" With Macros.

The following is the code for the Macro that I run. At this point, I can
make it run after I open the Workbook using the ALT F8 command but only in
the active Worksheet. I have to switch manually from Sheet to Sheet to
execute the Macro in all Sheets. I would like to call the Macro once and
have it run once in all Worksheets in the Workbook. The way I would like

to
run the Macro is manually, one time, after the Workbook is opened and have

it
run the Macro in each Worksheet in the Workbook.

' Update_Research_Data Macro
'
Range("A10").Select
Selection.EntireRow.Insert
Range("A9:F9").Select
Selection.Copy
Range("A10").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone,
SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Range("G9:U9").Select
Selection.Copy
Range("G10").Select
Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
Range("A9").Select
End Sub

Thanking you in advance for any help you can provide. It is greatly
appreciated.

--
SHD


"Bob Phillips" wrote:

It will run 7 time but you need to adjust your code for each individual

run
to address the correct sheet. I showed the basic mechanism earlier, but
without seeing your macro, I cannot absolutely state what would need
adjusting.

There is no need to activate each sheet, you can work on them without

doing
so.

--
HTH

Bob Phillips

(remove nothere from email address if mailing direct)

"YellowBird" wrote in message
...
Bob,

I'm not sure if you reviewed the reply I sent to Gary but basically I

had
the same result with your suggestion. The Marco ran 7 times which I

assume
was 1 for each Worksheet in the Workbook. It doesn't seem to be

switching
to
the other Worksheets.

Any other thoughts/suggestions are welcome.

Thanks.
--
SHD


"Bob Phillips" wrote:

For Each sh In ActiveWorkbook.Worksheets
Call MyMacro
Next sh

You might need to pass the sh object to the macro so that it can

work on
that, like this

For Each sh In ActiveWorkbook.Worksheets
Call MyMacro(sh)
Next sh

Sub MyMacro(pSheet as worksheet)
With pSheet
'...
End With
End Sub

--
HTH

Bob Phillips

(remove nothere from email address if mailing direct)

"YellowBird" wrote in message
...
I am trying to run a Macro (which works perfectly) in "All"

Worksheets
of
a
particular Workbook. I currently have seven Worksheets in the

Workbook
and
have to select each Worksheet and manually run the Macro. I will
eventually
have 50 (maybe more) Worksheets where the Macro will need to be

run.
I
have
used the "Private Sub" code in VBA but can only get it to execute

the
Macro
in the active Worksheet when the Workbook opens.

Is there code that could/would run the Macro in "All" the Sheets?

Sheet1,
Sheet2, Sheet3, Sheet4.

Any help would be greatly appreciated.

--
SHD








  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 13
Default Running Macro/VBA in multiple worksheets.

Gary,

I've tried the new code and it works except that instead of running the
Macro in each sheet, it runs the Macro 7 times which is the number of sheets
I currently have in the workbook.

The following is the code with your suggested addition.

Public Sub ProcessAllWorksheets()
Dim wk As Worksheet
For Each wk In Application.Worksheets

' Update_Research_Data Macro
'
Range("A10").Select
Selection.EntireRow.Insert
Range("A9:F9").Select
Selection.Copy
Range("A10").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone,
SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Range("G9:U9").Select
Selection.Copy
Range("G10").Select
Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
Range("A9").Select
Next wk
--
SHD


"Gary L Brown" wrote:

Public Sub ProcessAllWorksheets()
Dim wk As Worksheet
For Each wk In Application.Worksheets
'call your macro
Next wk
End Sub


HTH,
--
Gary Brown

If this post was helpful, please click the ''Yes'' button next to ''Was this
Post Helpfull to you?''.


"YellowBird" wrote:

I am trying to run a Macro (which works perfectly) in All Worksheets of a
particular Workbook. I currently have seven Worksheets in the Workbook and
have to select each Worksheet and manually run the Macro. I will eventually
have 50 (maybe more) Worksheets where the Macro will need to be run. I have
used the Private Sub code in VBA but can only get it to execute the Macro
in the active Worksheet when the Workbook opens.

Is there code that could/would run the Macro in All the Sheets? Sheet1,
Sheet2, Sheet3, Sheet4

Any help would be greatly appreciated.

--
SHD

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
how do you calculate a running balance in multiple worksheets? strawberryangel83 Excel Worksheet Functions 5 November 4th 08 02:41 PM
Running a macro in several worksheets Lisa Excel Discussion (Misc queries) 1 September 23rd 08 08:49 PM
Having a formula provide a running total from multiple worksheets sisko101 Excel Worksheet Functions 3 July 7th 06 10:06 PM
VBA to consolidate multiple worksheets to 1 worksheet in running sequence chongchingsoo[_5_] Excel Programming 0 November 11th 04 02:52 PM
VBA to consolidate multiple worksheets to 1 worksheet in running sequence chongchingsoo[_4_] Excel Programming 1 November 11th 04 02:27 PM


All times are GMT +1. The time now is 12:42 PM.

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

About Us

"It's about Microsoft Excel"