具有多个输入的调用模块

Call module with multiple inputs

我已尝试找到解决此问题的方法,但未能成功。

通用形式:

模块 1

Sub Source()
Call Module2.Run
End Sub

模块 2

Sub Run()
Value = 10
     Some code which uses Value as input
End Sub

我想要做的是能够在 Module1 中定义多个值,然后 运行 Module2.Run() 每个值。

模块 1

Sub Source()
Value = 10, 20, 30
Call Module2.Run (10)
Call Module2.Run (20)
Call Module2.Run (30)
End Sub

模块 2

Sub Run()
Value = Input from Module1.Source()
     Some code which uses Value as input
End Sub

或者类似的东西。任何投入将不胜感激。

你可以像这样通过参数传递参数

Sub Sub1 ()
    Dim myVal as Long
    myVal = 1000
    Sub2 (myVal) 'The "Call" is not necessary
End Sub


Sub Sub2 (myVal as Long) 'This sub requires an input to run (myVal)
    MsgBox myVal
End Sub

您可以创建一个数组,填充它并作为参数传递。 避免使用 SourceRun 之类的名称,这些名称已被 Excel;.

使用
Option Explicit
Sub Sour()
    Dim arr_1d() As Variant
    arr_1d = Array("val1", "val2", "val3")
    Dest arr_1d
End Sub

Sub Dest(arr_1d() As Variant)
    Dim y As Long
    For y = LBound(arr_1d) To UBound(arr_1d)
      Debug.Print arr_1d(y)
    Next
End Sub