将多个子程序合并为一个VBA子程序

3
我对VBA、这个论坛和编程都很陌生。我有一个工作表,通过谷歌搜索和调整某些代码行,达到了我的要求。
我的问题是我总共有三个子程序,必须逐步运行每个VBA脚本。我希望将所有三个VBA脚本合并为一个(第一步+第二步+第三步=一个子程序)。
请问如何将这些多个VBA脚本或子程序组合成一个单一的子程序,以便我只需运行一次VBA脚本而不是三次?
'---------Step1----------------------------------------
'----Run the macro press F5-----
'========================================================================
' DELETES ALL ROWS FROM F DOWNWARDS WITH THE WORDs " " IN COLUMN F
'========================================================================
    Sub DeleteRowWithContents()
    Last = Cells(Rows.Count, "F").End(xlUp).Row
    For i = Last To 1 Step -1
        If (Cells(i, "F").Value) = "Ja" Or (Cells(i, "F").Value) = "Unbearbeitet" Or (Cells(i, "F").Value) = "-" Or (Cells(i, "F").Value) = "" Then
    'Cells(i, "A").EntireRow.ClearContents ' USE THIS TO CLEAR CONTENTS BUT NOT DELETE ROW
            Cells(i, "A").EntireRow.Delete
        End If
    Next i
End Sub

'-------------------------------Step 2--------------------
'---Run the macro, press F5. The macro compares the row contents in column A and if found a match deletes one of the results--


Sub btest()
Dim LR As Long, i As Long
LR = Range("A" & Rows.Count).End(xlUp).Row
For i = LR To 2 Step -1
    If Range("A" & i).Value = Range("A" & i - 1).Value Then Rows(i).Delete
Next i
End Sub

'-----------------Step 3---------
'--------Delete Unwanted Columns and adjust the column width----
Sub sbVBS_To_Delete_EntireColumn_For_Loop()
Dim iCntr
Dim kCntr
Dim jCntr
For iCntr = 1 To 4 Step 1  
Columns(2).EntireColumn.Delete            '-----Del unwanted columns----
Next
For kCntr = 1 To 3 Step 1
Columns(3).EntireColumn.Delete
Next
For jCntr = 1 To 8 Step 1
Columns(4).EntireColumn.Delete
Next
ActiveSheet.Columns("A").Columnwidth = 20 '----Adjust Column width---
ActiveSheet.Columns("C").Columnwidth = 25
ActiveSheet.Columns("E").Columnwidth = 25
End Sub
3个回答

2
Sub Main()

    DeleteRowWithContents
    btest
    sbVBS_To_Delete_EntireColumn_For_Loop

End Sub

应该这样做。

你可以选择在其他子程序前加上Private修饰符,这样它们就不会出现在宏窗口中(在电子表格视图中按ALT+F8),你只能在那里列出Main

或者,你可以让其他三个步骤子程序采取虚拟可选参数来隐藏它们,以避免在宏对话框中显示。


1

@vba4all- 非常感谢。它的运行效果很棒。我该如何将这个问题标记为已解决?

@futureresearchers- 这是代码的样子。

Sub Main()
'========================================================================
' DELETES ALL ROWS FROM F DOWNWARDS WITH THE WORDs " " IN COLUMN F
'========================================================================
    Last = Cells(Rows.Count, "F").End(xlUp).Row
    For i = Last To 1 Step -1
        If (Cells(i, "F").Value) = "Ja" Or (Cells(i, "F").Value) = "Unbearbeitet" Or (Cells(i, "F").Value) = "-" Or (Cells(i, "F").Value) = "" Then
    'Cells(i, "A").EntireRow.ClearContents ' USE THIS TO CLEAR CONTENTS BUT NOT DELETE ROW
            Cells(i, "A").EntireRow.Delete
        End If
    Next i

    '---Run the macro, press F5. The macro compares the row contents in column A and if found a match deletes one of the results and the complete row--
    Dim LR As Long, x As Long
    LR = Range("A" & Rows.Count).End(xlUp).Row
    For x = LR To 2 Step -1
    If Range("A" & x).Value = Range("A" & x - 1).Value Then Rows(x).Delete
    Next x

   '--------Delete Unwanted Columns and adjust the column width----

    Dim lCntr
    Dim kCntr
    Dim jCntr
     For lCntr = 1 To 4 Step 1
    Columns(2).EntireColumn.Delete            '-----Del unwanted columns here the col b,c,d, e is to be deleted----
     Next
     For kCntr = 1 To 3 Step 1
    Columns(3).EntireColumn.Delete            '--enable or disable this loc if you dont wish to further delete cols---
    Next
    For jCntr = 1 To 8 Step 1
    Columns(4).EntireColumn.Delete            '--enable or disable this loc if you dont wish to further delete cols---
    Next
    ActiveSheet.Columns("A").ColumnWidth = 20 '----Adjust Column width---
    ActiveSheet.Columns("C").ColumnWidth = 25
    ActiveSheet.Columns("E").ColumnWidth = 25


End Sub

0

这其实非常简单。一旦您设置好了所有的子程序,请回到第一个子程序。在 End Sub 之前插入一行,输入后续子程序的名称。它们将按您在列表中放置它们的顺序依次运行。

因此,您的解决方案应该是:

Sub DeleteRowWithContents()
    Last = Cells(Rows.Count, "F").End(xlUp).Row
    For i = Last To 1 Step -1
        If (Cells(i, "F").Value) = "Ja" Or (Cells(i, "F").Value) = "Unbearbeitet" Or (Cells(i, "F").Value) = "-" Or (Cells(i, "F").Value) = "" Then
    'Cells(i, "A").EntireRow.ClearContents ' USE THIS TO CLEAR CONTENTS BUT NOT DELETE ROW
            Cells(i, "A").EntireRow.Delete
        End If
    Next i

'we insert the names of the subsequent subs to run here

btest

sbVBS_To_Delete_EntireColumn_For_Loop

End Sub

'-------------------------------Step 2--------------------
'---Run the macro, press F5. The macro compares the row contents in column A and if found a match deletes one of the results--
   
Sub btest()
Dim LR As Long, i As Long
LR = Range("A" & Rows.Count).End(xlUp).Row
For i = LR To 2 Step -1
    If Range("A" & i).Value = Range("A" & i - 1).Value Then Rows(i).Delete
Next i
End Sub

'-----------------Step 3---------
'--------Delete Unwanted Columns and adjust the column width----
Sub sbVBS_To_Delete_EntireColumn_For_Loop()
Dim iCntr
Dim kCntr
Dim jCntr
For iCntr = 1 To 4 Step 1  
Columns(2).EntireColumn.Delete            '-----Del unwanted columns----
Next
For kCntr = 1 To 3 Step 1
Columns(3).EntireColumn.Delete
Next
For jCntr = 1 To 8 Step 1
Columns(4).EntireColumn.Delete
Next
ActiveSheet.Columns("A").Columnwidth = 20 '----Adjust Column width---
ActiveSheet.Columns("C").Columnwidth = 25
ActiveSheet.Columns("E").Columnwidth = 25
End Sub

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接