How to apply macro across multiple worksheets within a workbook

M

murkaboris

Hello:

I have a macro in a workbook that I've created on the first worksheet. I
have 11 worksheets in the workbook and the same macro needs to run on each
worksheet.
Is there a way to do it automatically in the original macro or do I have to
run it every time separately on each worksheet.

Please help.
Thank you.

Monika
 
S

Sheeloo

You can use the macro on other sheets also assuming it does not worksheet
level code/procedures...

Steps:
Press ALT-F11 to open the VB Editor
Choose Insert->Module
and paste your macro

Now paste the following after changing testmacro to the name of your macro
sub runOnAllSheets
for each ws in worksheets
call testmacro
next ws
end sub
 
G

Gary''s Student

Say we have a macro that does stuff on a single sheet (the ActiveSheet)

Sub SingleSheet()
ActiveSheet.Range("A1").Value = "hello world"
End Sub

We want this done on ALL sheets. So we use a loop:

Sub AllTheSheets()
Dim s As Worksheet
For Each s In Worksheets
s.Range("A1").Value = "hello world"
Next
End Sub
 
M

murkaboris

Hello Sheeloo:

Thank you for your help.
Monika

Sheeloo said:
You can use the macro on other sheets also assuming it does not worksheet
level code/procedures...

Steps:
Press ALT-F11 to open the VB Editor
Choose Insert->Module
and paste your macro

Now paste the following after changing testmacro to the name of your macro
sub runOnAllSheets
for each ws in worksheets
call testmacro
next ws
end sub
 
M

murkaboris

Thank you for your help Gary
Monika

Gary''s Student said:
Say we have a macro that does stuff on a single sheet (the ActiveSheet)

Sub SingleSheet()
ActiveSheet.Range("A1").Value = "hello world"
End Sub

We want this done on ALL sheets. So we use a loop:

Sub AllTheSheets()
Dim s As Worksheet
For Each s In Worksheets
s.Range("A1").Value = "hello world"
Next
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top