将多个子组合到一个VBA子组中

时间:2014-10-16 12:10:00

标签: vba excel-vba excel

我对VBA,这个论坛和一般的编程都很陌生。我有一个工作表,我已经设法谷歌并根据我的要求调整某些代码行。

我的问题是我总共有三个潜艇,必须逐步运行每个VBA脚本。我希望将所有三个VBA脚本合二为一。 (步骤1 +步骤2 +步骤3 = All in one Sub)

你能告诉我如何在一个单独的sub的umbrealla下组合多个VBA脚本或Subs,这样我只需要运行一次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

2 个答案:

答案 0 :(得分:1)

Sub Main()

    DeleteRowWithContents
    btest
    sbVBS_To_Delete_EntireColumn_For_Loop

End Sub

应该这样做。

您可以选择使用Private修饰符为其他潜点添加前缀,这样它们就不会出现在宏窗口中(电子表格视图中的 ALT + F8 )你只在那里列出了Main

或者,您可以使用其他3个步骤子接受虚拟可选参数来隐藏它们从宏对话框中。

答案 1 :(得分:1)

@ vba4all-非常感谢你。它就像一个魅力。我如何将此问题放在Solved上?

@ 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