Solved

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

Posted on 2014-04-14
16
421 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 34

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
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
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 34

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

Office 365 Training for Admins - 7 Day Trial

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

Question has a verified solution.

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

Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

626 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