Solved

How to create a macro to run 4 different macros on 4 different sheets?

Posted on 2014-04-14
16
418 Views
Last Modified: 2014-04-15
Right now I have a macro that clears certain cells on a sheet.  I have 4 sheets, 4 macros and a button on each sheet to run each macro.  I would like to create a macro that will run all 4 macros for their respective sheet and assign it to a button on my main sheet.

Sheet #            Sheet Name      Name of macro
Sheet1            HPSM            Clear_Cells
Sheet7            EVIP-VIP                      Clear_Cells_EVIP
Sheet8            PCA            Clear_Cells_PCA
Sheet9            PCR            Clear_Cells_PAR
0
Comment
Question by:kbay808
  • 7
  • 5
  • 2
  • +1
16 Comments
 
LVL 43

Expert Comment

by:Saqib Husain, Syed
ID: 40000907
Use

Sheet1.Clear_Cells
Sheet7.Clear_Cells_EVIP
Sheet8.Clear_Cells_PCA
Sheet9.Clear_Cells_PAR

or

Sheets(HPSM).Clear_Cells
Sheets(EVIP-VIP).Clear_Cells_EVIP
Sheets(PCA).Clear_Cells_PCA
Sheets(PCR).Clear_Cells_PAR
0
 
LVL 33

Expert Comment

by:Norie
ID: 40001186
Try this.
Dim arrMacros As Variant
Dim I As Long

    arrMacros = Array("Sheet1.Clear_Cells", "Sheet7.Clear_Cells_EVIP", "Sheet8.Clear_Cells_PCA", "Sheet9.Clear_Cells_PAR")

    For I = LBound(arrMacros) To UBound(arrMacros)
        Application.Run arrMacros(I)
    Next I

Open in new window

0
 

Author Comment

by:kbay808
ID: 40002361
No joy for all 3.  I attached a screen shot for all 3 for your review
Syed-Fix-1.jpg
Syed-Fix-2.jpg
Imnorie-Fix.jpg
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 43

Expert Comment

by:Saqib Husain, Syed
ID: 40002384
Sheets("HPSM").Clear_Cells
Sheets("EVIP-VIP").Clear_Cells_EVIP
Sheets("PCA").Clear_Cells_PCA
Sheets("PCR").Clear_Cells_PAR
0
 

Author Comment

by:kbay808
ID: 40002479
I’m still getting an error.  I'm running Office 2010 just in case that makes a difference.
Syed-Fix-3.jpg
0
 
LVL 33

Expert Comment

by:Norie
ID: 40002689
Can you post the code for the macros and/or attach a sample workbook?
0
 

Author Comment

by:kbay808
ID: 40002903
As requested
Sample-Workbook.xlsm
0
 
LVL 15

Expert Comment

by:DrTribos
ID: 40003066
I think this is what you are asking for:

Sub callMacros()
    Call Module4.Clear_Cells_EVIP
    Call Module3.Clear_Cells_PCR
    Call Module2.Clear_Cells_PCA
    Call Module1.Clear_Cells
End Sub

Open in new window


Note that you can combine Modules 1-4 without harming your project (unless you have specific reasons for keeping them separate).

Hope it helps
0
 

Author Comment

by:kbay808
ID: 40003077
DrTribos

That will run all of the macros on the same sheet.  I only want to run the specific macro for each specific sheet.
0
 

Author Comment

by:kbay808
ID: 40003085
As a different approach, can the sheet be assigned in the individual macros?  That way it does not matter where the macro is ran from.
0
 
LVL 15

Expert Comment

by:DrTribos
ID: 40003115
Yes that would be the way to do it... you can use named ranges or address the sheet:

ActiveWorkbook.Sheets("EVIP-VIP").Range("C10").ClearContents
0
 
LVL 15

Accepted Solution

by:
DrTribos earned 500 total points
ID: 40003118
I'd suggest changing your macro to a single macro:
Sub CleanUp()

With ActiveWorkbook
 .Sheets("EVIP-VIP").Range("C10").ClearContents
 .Sheets("PCR").Range("B2:B3").ClearContents
    With .Sheets("PCA")
     .Range("B4").ClearContents
     .Range("B6").ClearContents
     .Range("B7").ClearContents
     .Range("B8").ClearContents
    End With
 .Sheets("HPSM").Range("B1:B11").ClearContents
End With

End Sub

Open in new window



The "With .... End With" blocks are a kind of shorthand & code optimisation; they group things that are in the same place so they only need to be found once.
0
 
LVL 15

Expert Comment

by:DrTribos
ID: 40003122
By the way... instead of
     .Range("B4").ClearContents
     .Range("B6").ClearContents
     .Range("B7").ClearContents
     .Range("B8").ClearContents

Open in new window

you might as well just have
.Range("B4:B8").ClearContents

Open in new window

particularly if B5 will be blank anyway...
0
 

Author Comment

by:kbay808
ID: 40003134
It worked!!!  Thank you very much.
0
 

Author Closing Comment

by:kbay808
ID: 40003135
Great work!
0
 
LVL 15

Expert Comment

by:DrTribos
ID: 40003137
Glad to help, thanks for the grade  :-)
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

791 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question