我有一系列子程序(Sub1
,Sub2
,Sub3
等)。我想根据用户定义的值调用这些子例程的子集。例如,子7到13。
我想过使用基于子程序名称中的数字的循环,但它似乎在VBA中不起作用。有没有人有建议?
示例代码:
Sub test()
Dim i As Integer
Dim Start As Integer
Dim End As Integer
Start = CEM_Exec.Range("User_Start")
End = CEM_Exec.Range("User_End")
For i = Start To End
Call Sub"i"
Next i
End Sub
答案 0 :(得分:2)
假设所有预期的程序都在模块1中,
Sub test()
Dim i As Integer
Dim intStart As Integer
Dim intEnd As Integer
intStart = CEM_Exec.Range("User_Start")
intEnd = CEM_Exec.Range("User_End")
For i = intStart To intEnd
Application.run "Module1.Sub" & i
Next i
End Sub
答案 1 :(得分:1)
我认为您问题的简单解决方案是在Select Case
循环中使用For
。
Sub test()
Dim i As Integer, Start_Num As Integer, End_Num As Integer
Start_Num = CEM_Exec.Range("User_Start")
End_Num = CEM_Exec.Range("User_End")
For i = Start_Num To End_Num
Select Case i
Case 1
Call Sub1
Case 2
Call Sub2
Case 3
Call Sub3
'and so on
'
'
'
'
Case Else
'Error message
End Select
Next i
End Sub
答案 2 :(得分:1)
您可以使用Application.Run
方法以及过程名称:
注意:End
是VBA中的保留字,因此您使用名为iEnd的变量(我已更新Start
和End
是更安全的iStart
和iEnd
。
Sub test()
Dim i As Integer
Dim iStart As Integer
Dim iEnd As Integer
iStart = CEM_Exec.Range("User_Start")
iEnd = CEM_Exec.Range("User_End")
For i = iStart To iEnd
Application.Run "Sub" & i
Next i
End Sub