![VBA - 如何在同一个宏内循环执行程序?](https://linux22.com/image/1593559/VBA%20-%20%E5%A6%82%E4%BD%95%E5%9C%A8%E5%90%8C%E4%B8%80%E4%B8%AA%E5%AE%8F%E5%86%85%E5%BE%AA%E7%8E%AF%E6%89%A7%E8%A1%8C%E7%A8%8B%E5%BA%8F%EF%BC%9F.png)
如何循环程序?我不喜欢“调用”子程序,因为它太不稳定/容易崩溃,而且会使我的屏幕闪烁……那么如何在同一个宏内循环程序……?示例:
Sub LoopBase()
First, Do base
Then do A
Then do Base
Then do B
Then do Base
Then do C
Then do Base
Then do D
Then do Base
Then do procDone
Exit Sub
Same example, different format....
Sub LoopBase()
procBase:
Code....
procA:
Code....
Goto procBase (and then goto next ProcB)
procB:
Code....
Goto procBase (and then goto next ProcC)
procC:
Code....
Goto procBase (and then goto next ProcD)
procD:
Code....
Goto procBase (and then goto next procDone)
procDone:
Code...
End Sub
答案1
以下是根据您的描述结合 for 循环和实现代码流程的一种方法选择案例:
Const MaxRunCount = 4
Dim RunCount As Long
For RunCount = 1 To MaxRunCount
' insert code "base" here
Select Case RunCount
Case 1
' put procA here
Case 2
' put procB here
Case 3
' put procC here
Case 4
' put procD here
End Select
Next RunCount
我不喜欢“呼叫”子程序,因为它太不稳定/容易崩溃,而且会使我的屏幕闪烁
正如已经评论的那样,这不是由于调用子程序,而是它包含的内容,所以我的解决方案最终可能无法解决您的问题。