具有多个输入的呼叫模块

时间:2018-09-29 21:02:51

标签: vba excel-vba input subroutine

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

通用形式:

Module1

Sub Source()
Call Module2.Run
End Sub

Module2

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

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

Module1

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

Module2

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

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

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

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

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