0
votes

I have 3 macros that I would like to combine into one that runs sequentially. The first bit I would like to run refreshes all of the data in an excel workbook, the next two delete specific rows on a sheet. The three macros I have work individually, but I would like to combine all three so the end user can run them all at once. I think the issue I'm running into is that I incorrectly made these macros to run on specific sheets, not modules. I'm stuck on combining them, since the macros are already in different sheets.

The First Macro:

Sub Macro1()
   ActiveWorkbook.RefreshAll
   Sheets("ManagementDashboard").Select
End Sub

The Second Macro (Runs in the sheet named "RawDataPltzr")

Sub DeleteRows()
   Last = Cells(Rows.Count, "c").End(xlUp).Row
   For i = Last To 1 Step -1
           'if cell value is less than 100
   If (Cells(i, "c").Value) < 100 Then
           'delete entire row
        Cells(i, "c").EntireRow.Delete
     End If
   Next i
End Sub

The Third Macro (Runs in the sheet named "RawDataLoader", also the same as 2nd macro, just has to run in different sheet)

Sub DeleteRows()
  Last = Cells(Rows.Count, "c").End(xlUp).Row
  For i = Last To 1 Step -1
            'if cell value is less than 100
    If (Cells(i, "c").Value) < 100 Then
            'delete entire row
        Cells(i, "c").EntireRow.Delete
    End If
  Next i
End Sub

At the end, I want the sheet "ManagementDashboard" to be selected, like in the first macro, but I need the second and third macro to run before it does so. Any help would be appreciated!

2
Fully qualify your sheet references. The way the code is now, it assumes ActiveSheet, which can cause a lot of problems.braX

2 Answers

2
votes

Only code relating to worksheet events should be in the worksheet modules

  1. Create a new module (right click > insert > module)
  2. Move all three subs into the new module (rename those with the same name)
  3. Anywhere you have Cells you need to refer to the appropriate worksheet first. E.g. Worksheets("RawDataLoader").Cells()
  4. Add a new sub as per below which will run all 3 for you

Sub RunAll()
    Macro1
    DeleteRowsPltzr
    DeleteRowsLoader
    ThisWorkbook.Worksheets("ManagementDashboard").Activate    
End Sub

Make sure you understand that method properly. At which point you can combine all three subs into this one sub (no need to call them separately)

Sub RunAll()
    ThisWorkbook.RefreshAll

    Dim ws As Worksheet
    For Each ws In ThisWorkbook.Worksheets(Array("RawDataPltzr", "RawDataLoader"))
        Last = ws.Cells(ws.Rows.Count, "c").End(xlUp).Row
        For i = Last To 1 Step -1
                  'if cell value is less than 100
          If (ws.Cells(i, "c").Value) < 100 Then
                  'delete entire row
              ws.Cells(i, "c").EntireRow.Delete
          End If
        Next i
    Next ws

    ThisWorkbook.Worksheets("ManagementDashboard").Activate
End Sub
0
votes

Anytime you don't reference a location excel takes whatever is active at the time and runs with it. For example Cells(i, "C").EntireRow.Delete is read as ActiveWorkbook.ActiveWorksheet.Cells(i,"C").EntireRow.Delete. By using Dim we don't have to select or active any worksheets or cells. Reference Here for more information. I left the final .Activate in there but it's very bad practice to use. Throw this code into its own module and it should run fine.

Sub All_Three()

    Dim wbk As Workbook
    Dim MD As Worksheet, RDP As Worksheet, RDL As Worksheet, ws As Worksheet
    Dim Last As Long, i As Long, myCount As Long

    Set wbk = ActiveWorkbook
    Set MD = wbk.Worksheets("ManagementDashboard")
    Set RDP = wbk.Worksheets("RawDataPltzr")
    Set RDL = wbk.Worksheets("RawDataLoader")

    'Macro1
    wbk.RefreshAll

    For myCount = 1 To 2
        Select Case myCount
            Case 1
                Set ws = RDP
            Case 2
                Set ws = RDL
        End Select

        'Macro 2 and 3
        With ws
            Last = .Range("C" & .Rows.Count).End(xlUp).Row
            For i = Last To 1 Step -1
                If .Cells(i, "C").Value < 100 Then
                    .Cells(i, "C").EntireRow.Delete
                End If
            Next i
        End With
    Next myCount

    MD.Activate

End Sub