VBA Excel,循环遍历变量 [英] VBA Excel, loop through variables
问题描述
我现在试图在下面修复几个小时,我想我一直都坚持下去.到这一点.
Was trying to fix below for couple of hours now and I think I'm just permanently stuck with it. To the point.
当前代码:
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 我希望能够以与返回类型相同的方式返回变量名:TypeName().因此,对于第一个循环,msgbox会说:i1,依此类推.我正在尝试.name等的不同组合,但似乎一直让我失望.
Question 1 I would like to be able to return variable name, in the same way as you can return type: TypeName(). So for first loop msgbox would say: i1 and so on. I was trying different combination of .name etc but seems to fail me all the time.
问题2 问题1的原因是,下一步,我想根据变量名称在原始值中添加一些内容.
Question 2 Reason for question 1 is, as a next step I would like to add something to the original value depending on the variable name.
希望我希望有这样的东西(显然,此代码不起作用,仅用于演示):
Hopefully I would like to have something like this (obviously this code does not work, it's only for presentation):
For Each vCurrent In vGen
vCurrent.name = vcurrent + iSomeNumber
Next
iSomeNumber将从程序的另一部分进入,然后稍后我可以检索更新的单个变量(即i1不再是值1,而是1 + iSomeNumber).
Where iSomeNumber will be coming in from another part of the program and then i could retrieve updated individual variables later on (i.e. i1 will no longer be value 1 but 1 + iSomeNumber).
希望我已经清楚地解释了我的问题,如果您需要任何其他信息,请告诉我.
Hopefully I've explained my problem plainly, if you would require any additional info please let me know.
我还要再待4个小时,所以我的回复可能会有点延迟.
Also I will be AFK for another 4 hours, so my reply might be a little bit delay.
在此先感谢您的帮助!
推荐答案
您可以查看Scripting.Dictionary
对象:它允许您将值与字符串"keys"相关联
You can look at the Scripting.Dictionary
object: it allows you to associate values with 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
这篇关于VBA Excel,循环遍历变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!