简体   繁体   English

将变量值分配给变量键

[英]Assign a variable value to a variable key

I need to create variables on the fly in Excel VBA, as for example users type Name and Age inside variable NAME and AGE. 我需要在Excel VBA中动态创建变量,例如,用户在变量NAME和AGE中键入名称和年龄。 Suppose user type PETER in NAME and 34 in AGE, I want VBA to create a variable PETER with contents 34. This can be done easily in PHP. 假设用户在NAME中键入PETER,在AGE中键入34,我希望VBA创建一个内容为34的变量PETER。这可以在PHP中轻松完成。

I was able to make part of it works: 我能够使其一部分起作用:

Person = Cells(10, 1).Value 'suppose it is "PETER"
Age = Cells(11, 1).Value    'suppose it is "34"

Creating the Variable 创建变量

Names.Add Name:=Person, RefersTo:=Age

Retrieving it 检索它

Debug.Print Person              ' prints PETER
Debug.Print Evaluate(Person)    ' prints 34
Debug.Print PETER               ' Variable PETER does not exist

The above doesn't help me, since it is the same as creating a Variable "NAME" with contents PETER, when evaluated shows 34. I need Variable PETER with contents 34. 上面的内容对我没有帮助,因为它与创建内容为PETER的变量“ NAME”相同,当评估显示为34时。我需要内容为34的Variable PETER。

The problem is; 问题是;

PERSON="PETER"
AGE="34"
Name.Add Name:=PERSON RefersTo:=Age

creates a variable PERSON=34, not PETER=34. 创建一个变量PERSON = 34,而不是PETER = 34。

Name.Add Name:=Evaluate(Person) ... does not work. Name.Add Name:=Evaluate(Person) ...不起作用。

Any help please? 有什么帮助吗?

Update from 03/22/2019 1:38pm 从03/22/2019 1:38 pm更新

Following some tips and discoveries, Dictionary is what solve my problem. 按照一些提示和发现,字典可以解决我的问题。

Sub T1() 
  Dim MAGIC As New Scripting.Dictionary 
  AnyVar = "R10"               'sets [AnyVar] = "R10"
  Magic(AnyVar) = 25           'sets [R10] = "25" 
  Magic("ARTEN") = "R10"       '[ARTEN] points to R10
  Magic("R10") = 33            'sets [R10] = "33"
  N5 = Magic("ARTEN")          'sets N5 = "R10"
  N6 = Magic(N5)               'sets N6 = [R10] (33)
  Debug.Print N5 & " = " & N6 
End Sub 

It printed "R10 = 33", exactly what I was willing to have. 它打印出“ R10 = 33”,正是我所希望的。 So now, with dictionary I can create variables on fly and find or change their values at easy. 因此,现在,借助字典,我可以即时创建变量并轻松查找或更改它们的值。

To create a new entry on Magic (dictionary): 要在Magic(词典)上创建一个新条目:

Magic.Add key:="name", Item:=33

If a text between quotes, the new entry will be that text. 如果引号之间是文本,则新条目将是该文本。 If without quotes, it assumes it is a variable containing the name/value. 如果不带引号,则假定它是一个包含名称/值的变量。

V25 = "foo"
V26 = 33
Magic.Add key:=V25, Item:=V26

Will create an entry name "foo" with contents 33. 将创建一个包含内容33的条目名称“ foo”。

To change any existent entry, just 要更改任何现有条目,只需

V25 = "foo"
V26 = 33
V29 = "fee"
V30 = "faa"
Magic(V25) = V26                '[foo] = 33
Magic("foo") = 38               '[foo] = 38
Magic(V29) = 39                 '[fee] = 39
Magic(V25) = Magic(V29) + 1     '[foo] = [fee]+1 = 40 
dictionary(V30) = V25           '[faa] = [V25] = "foo"
debug.print Magic(V30)          'prints "foo"
debug.print Magic(Magic(V30))   'prints 40
V40 = "Paul"
V41 = "Smith"
Magic(V40) = V41
debug.print Magic("Paul")       'print Smith

If entry name "foo" exists, it will change the contents to the contents of V26, if the entry does not exist, it creates it. 如果条目名称“ foo”存在,它将内容更改为V26的内容,如果条目不存在,它将创建它。

So, you don't need to use the formal way to create an entry. 因此,您不需要使用正式的方法来创建条目。

Magic.Add key:=V25, Item:=V26

You could also use a simple class named cPerson 您还可以使用一个名为cPerson的简单类

Option Explicit

Public pName As String
Public pAge As Long

An example might look like that 一个例子可能像这样

Option Explicit

Sub TestIt()
Dim dict As New Scripting.Dictionary
Dim person As cPerson
Dim key As Variant

    Set person = New cPerson
    With person
        .pName = "Peter"
        .pAge = 34
        dict.Add .pName, person
    End With


    Set person = New cPerson
    With person
        .pName = "John"
        .pAge = 43
        dict.Add .pName, person
    End With

    For Each key In dict.Keys
    With dict(key)
        Debug.Print .pName, .pAge
    End With
    Next

    Debug.Print dict("Peter").pAge
    Debug.Print dict("John").pAge

End Sub

Update Within Excel you need to set a reference to the VB script run-time library. 更新在Excel中,您需要设置对VB脚本运行时库的引用。

  • To reference this library, load the Visual Basic Editor (ALT+F11) 若要引用此库,请加载Visual Basic编辑器(ALT + F11)
  • Select Tools > References from the drop-down menu 从下拉菜单中选择工具>参考。
  • A listbox of available references will be displayed 将显示可用参考的列表框
  • Tick the check-box next to 'Microsoft Scripting Runtime' 勾选“ Microsoft脚本运行时”旁边的复选框
  • The full name and path of the scrrun.dll file will be displayed below the listbox scrrun.dll文件的全名和路径将显示在列表框下方
  • Click on the OK button. 单击确定按钮。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM