Solved

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

Posted on 2014-04-14
16
409 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
 
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 14

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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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 14

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 14

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 14

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 14

Expert Comment

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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

PaperPort has a feature called the "Send To Bar". It provides a convenient, drag-and-drop interface for using other installed software, such as Microsoft Office. However, this article shows that the latest Office 2016 apps (installed with an Office …
No matter the version of Windows you are using, you may have some problems with Windows Search running too slow or possibly not running at all. Before jumping into how you can solve this issue, just know there are many other viable alternative deskt…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
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…

760 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now