gpt4 book ai didi

vba - 将多个 Subs 合并为一个 VBA Sub

转载 作者:行者123 更新时间:2023-12-04 20:15:53 27 4
gpt4 key购买 nike

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

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

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

最佳答案

Sub Main()

DeleteRowWithContents
btest
sbVBS_To_Delete_EntireColumn_For_Loop

End Sub

应该做。

或者,您可以在其他潜艇前加上 Private修饰符,因此它们不会出现在宏窗口中(电子表格 View 中的 ALT+F8)并且您只有 Main列在那里。

或者,您可以让其他 3 个 step-subs 采用虚拟可选参数以将它们从“宏”对话框中隐藏。

关于vba - 将多个 Subs 合并为一个 VBA Sub,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26404058/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com