Solved

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

Posted on 2014-04-14
16
417 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

777 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