VBA Excel,通过variables循环

现在正试图解决几个小时以下,我想我只是永久坚持下去。 重点。

当前代码:

Sub ttttt() Dim i1 As Integer, i2 As Integer, i3 As Integer, i4 As Integer Dim vGen As Variant Dim vCurrent As Variant i1 = 1 i2 = 20 i3 = 300 i4 = 4000 vGen = Array(i1, i2, i3, i4) For Each vCurrent In vGen MsgBox vCurrent Next End Sub 

问题1我想能够返回variables名称,就像你可以返回types:TypeName()。 所以对于第一个循环msgbox会说:i1等等。 我正在尝试.name等不同的组合,但似乎总是让我失望。

问题2问题1的原因是,作为下一步,我想根据variables名称添加一些原始值。

希望我想这样的东西(显然这个代码不起作用,只是为了演示):

 For Each vCurrent In vGen vCurrent.name = vcurrent + iSomeNumber Next 

iSomeNumber将从程序的另一部分来,然后我可以稍后检索更新后的个体variables(即i1将不再是值1,而是1 + iSomeNumber)。

希望我已经解释清楚我的问题,如果你需要任何额外的信息,请让我知道。

另外我会再AFK 4个小时,所以我的答复可能会稍微延迟一点。

预先感谢您的任何帮助!

您可以查看Scripting.Dictionary对象:它允许您将值与string“keys”

 Dim d, k Set d = CreateObject("Scripting.Dictionary") d.Add "A", 10 d.Add "B", 20 d.Add "C", 30 For Each k in d Debug.print k, d(k) Next k d("B") = d("B") + 100 

好主意Tim W.

稍作修改。 尝试运行这一个:

 Sub ttttt() Set Dict = CreateObject("Scripting.Dictionary") Dict.Add "A1", 10 Dict.Add "B1", 20 Dict.Add "B2", 30 Dict.Add "C1", 40 ADDValue1 = 50 ADDValue2 = 100 ADDValue3 = 150 For Each k In Dict If k Like ("*A*") Then Debug.Print Dict(k) + ADDValue1 End If If k Like ("*B*") Then Debug.Print Dict(k) + ADDValue2 End If If k Like ("*C*") Then Debug.Print Dict(k) + ADDValue3 End If Next k End sub