将3个VBA宏组合成1个(不同工作表上的2个删除行,1个刷新数据)

时间:2017-10-05 12:17:00

标签: excel vba excel-vba

我有3个宏,我想组合成一个顺序运行的宏。我想要运行的第一个位刷新excel工作簿中的所有数据,接下来的两个删除工作表上的特定行。这三个宏我单独工作,但我想将所有三个结合起来,以便最终用户可以同时运行它们。我认为我遇到的问题是我错误地使这些宏在特定工作表上运行,而不是在模块上运行。我坚持组合它们,因为宏已经在不同的表格中。

第一个宏:

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

第二个宏(在名为&#34的表格中运行; 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

第三个宏(在名为&#34的工作表中运行; RawDataLoader&#34;,也与第二个宏相同,只需在不同的工作表中运行)

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

最后,我想要一张表&#34; ManagementDashboard&#34;要像第一个宏一样被选中,但我需要第二个和第三个宏才能运行。任何帮助将不胜感激!

2 个答案:

答案 0 :(得分:2)

只有与工作表事件相关的代码应该在工作表模块中

  1. 创建新模块(右键单击&gt;插入&gt;模块)
  2. 将所有三个子组件移动到新模块中(重命名具有相同名称的子组件)
  3. 您拥有Cells的任何地方,您需要先参考相应的工作表。例如。 Worksheets("RawDataLoader").Cells()
  4. 添加一个新的sub,如下所示,将为你运行所有3
  5. Sub RunAll()
        Macro1
        DeleteRowsPltzr
        DeleteRowsLoader
        ThisWorkbook.Worksheets("ManagementDashboard").Activate    
    End Sub
    

    确保您正确理解该方法。此时,您可以将所有三个子组合成这一个子组(无需单独调用它们)

    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
    

答案 1 :(得分:0)

任何时候你都没有引用一个位置excel会接受当时的任何活动并随之运行。例如,Cells(i, "C").EntireRow.Delete读为ActiveWorkbook.ActiveWorksheet.Cells(i,"C").EntireRow.Delete。使用Dim我们不必选择或激活任何工作表或单元格。有关详细信息,请参阅Here。我把最后的.Activate留在了那里,但这是非常糟糕的做法。将此代码放入自己的模块中,运行正常。

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