使用sub var进入其他sub visual basic

时间:2013-03-28 04:56:08

标签: vba variables

如何通过在其他子中调用它来从sub获取var值?像

  Sub test()
    Dim a As Integer
    a = 1
  End Sub
  Sub testshow()
    MessageBox.Show(test.a)
  End Sub

1 个答案:

答案 0 :(得分:1)

在VBA中(您的代码指明),您需要将Sub更改为Function,这将是:

Function test()
    Dim a As Integer
    a = 1
    test = a
End Function

Sub testshow()
   MsgBox test
End Sub

评论后编辑:如果您使用多个变量,那么:

Function test(whichVar)
    Dim a As Integer
    If whichVar = 1 then
        a = 100
    ElseIf whichVar = 2 Then
        a = 200
    'etc...
    End if

    test = a
End Function

Sub testshow()
   MsgBox test(2)    'will give you 200
End Sub