Passing parameter into function to create variable name

送分小仙女□ 提交于 2020-01-25 06:53:05

问题


I want to be able to use some global variables in my Workbook and in the ThisWorkbook Code I have declared the following varaibles

Public position_1 as string
Public position_2 as string

If I want to see the value of those variables I believe the need to be fully qualified so

Debug.Print ThisWorkbook.position_1
Debug.Print ThisWorkbook.position_2

I have written a UDF which I will pass in an integer to represent which variable I am looking for. I will only be passing in a single number and not a full variable name. I am trying to find a way to use this Integer to concatenate with "position_" to display the value of the global variable, ThisWorkbook.position_1, ThisWorkbook.position_2, etc.

Is this possible?

 Function Test_Global_Var(position as Integer)
            Dim variable_name As String
            variable_name = "position_" & position
            Debug.Print ThisWorkbook.variable_name
        End Function

So when I call

Test_Global_Var(1) 

my immediate window should displayed the value of

ThisWorkbook.position_1

回答1:


The code below produces the following debug output

2 values defined.
ThisWorkbook.Position(0)
First Value
ThisWorkbook.Position(1)
Second Value

It uses a private array in the workbook named m_position. The contents are accessed by a global property ThisWorkbook.Position(index).

In a module have the following code:

Option Explicit

Public Sub Test()    
    If ThisWorkbook.NoValues Then
        ThisWorkbook.FillValues "First Value", "Second Value"
    End If

    Debug.Print CStr(ThisWorkbook.Count) & " values defined."

    Test_Global_Var 0
    Test_Global_Var 1
End Sub

Public Sub Test_Global_Var(ByVal index As Long)
    ' Part of a UDF
    Debug.Print "ThisWorkbook.Position(" & CStr(index) & ")"
    Debug.Print ThisWorkbook.Position(index)

End Sub

In ThisWorkbook have the following code:

Option Explicit

Private m_position() As Variant

Private Sub Workbook_Open()
    Call DefaultValues
End Sub


Public Property Get Position(ByVal index As Long) As Variant
    Position = m_position(index)
End Property

Public Sub DefaultValues()
    m_position = Array("First", "Second")
End Sub

Public Sub FillValues(ParamArray args() As Variant)
    m_position = args
End Sub

Public Property Get Count() As Long
    Count = UBound(m_position) - LBound(m_position) + 1
End Property

Public Property Get NoValues() As Boolean
    On Error GoTo ArrUndefined:
    Dim n As Long
    n = UBound(m_position)
    NoValues = False
    On Error GoTo 0
    Exit Sub
ArrUndefined:
    NoValues = True
    On Error GoTo 0
End Property

PS. In VBA never use Integer, but instead use Long. Integer is a 16bit type, while Long is the standard 32bit type that all other programming languages consider as an integer.




回答2:


It is possible to consider a global dictionary variable and pass data through it from the UDF. First add reference to Microsoft Scripting Runtime:

Thus, build the dictionary like this:

Public myDictionary As Dictionary

To initialize the myDictionary variable, consider adding it to a Workbook_Open event:

Private Sub Workbook_Open()
    Set myDictionary = New Dictionary
End Sub

Then the UDF would look like this:

Public Function FillDicitonary(myVal As Long) As String

    If myDictionary.Exists(myVal) Then
        myDictionary(myVal) = "position " & myVal
    Else
        myDictionary.Add myVal, "position " & myVal
    End If
    FillDicitonary = "Filled with " & myVal

End Function

And it would overwrite every key in the dictionary, if it exists. At the end, the values could be printed:

Public Sub PrintDictionary()

    Dim myKey As Variant
    For Each myKey In myDictionary
        Debug.Print myDictionary(myKey)
    Next

End Sub


来源:https://stackoverflow.com/questions/58734466/passing-parameter-into-function-to-create-variable-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!