使用一个变量来调用第二个

时间:2011-12-21 13:39:32

标签: variables vba

我有很多数组:

mIOSconfig = Array("  ! *** SRMS ***  ", "int *** PORT ***")
mCATOSconfig = Array("  ! *** SRMS ***  ", "set port name *** DESC *** *** PORT ***   ")

我希望使用外部用户输入来调用它们。

因此,假设用户输入“IOS”,我想在将来的代码中使用mIOSconfig

所以让我们假设用户的输入存储在变量x

 x="IOS"
    usedarray = "m" + x + "config"

somevarible = somefunction(usedarray)

msgbox (somevarible)

但是这不起作用,因为usedarray变量是一个字符串,而这是传递给函数而不是数组。如何使用创建的usedarray变量将在开始时创建的数组传递给函数?

干杯

亚伦

3 个答案:

答案 0 :(得分:1)

作为一种解决方法,您可以使用集合来执行您想要的操作。使用“IOS”,“CATOS”等作为集合的键,创建一个Collection对象并将.Add数组添加到集合中。

正如@Justin在评论中指出的那样,VBA不支持你特别想做的事情。

答案 1 :(得分:1)

Sub GetOptionsByName()

    Dim opts As Object, strOpt As String
    Set opts = CreateObject("scripting.dictionary")

    'load the various alternatives
    opts.Add "IOS", Array("  ! *** SRMS ***  ", "int *** PORT ***")
    opts.Add "CATOS", Array("  ! *** SRMS ***  ", _
                            "set port name *** DESC *** *** PORT ***   ")

    strOpt = "IOS"

    Debug.Print Join(opts(strOpt), "::")


End Sub

答案 2 :(得分:0)

为什么不只是根据输入,从数据库或配置文件中填充一个数组?

然后在整个代码中使用相同的数组名称?

下面我在VB.Net中敲了一下但是看不出为什么不是VBA。你为什么要让用户输入免费文字?为什么不是comboBox /可用配置列表?

Dim myStrings As New List(Of String)
Private Enum ArrayType
    IOS = 1
    CATOS = 2
End Enum
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim strInput As String
    Dim theInputtedType As ArrayType

    strInput = InputBox("Enter Value", "Enter the value")

    Select Case strInput
        Case Is = "IOS"
            theInputtedType = ArrayType.IOS
        Case Is = "CATOS"
            theInputtedType = ArrayType.CATOS
        Case Else
            MessageBox.Show("Value not recognised")
            Exit Sub
    End Select

    PopulateMyList(theInputtedType)

    For Each s As String In myStrings
        MessageBox.Show(s)
    Next
End Sub

Private Sub PopulateMyList(ByVal InputtedType As ArrayType)
    Select Case InputtedType
        Case ArrayType.IOS
            'This would be a config read or DB read!!!!
            myStrings.Add("  ! *** SRMS ***  ")
            myStrings.Add("int *** PORT ***")
        Case ArrayType.CATOS
    End Select
End Sub