VBA Excel,循环变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23226630/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 02:51:33  来源:igfitidea点击:

VBA Excel, loop through variables

excelvbaloops

提问by dce

Was trying to fix below for couple of hours now and I think I'm just permanently stuck with it. To the point.

现在试图在下面修复几个小时,我想我只是永久地坚持下去。说到点子上了。

Current code:

当前代码:

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

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.

问题 1 我希望能够以与返回类型相同的方式返回变量名称:TypeName()。所以对于第一个循环 msgbox 会说: i1 等等。我正在尝试 .name 等的不同组合,但似乎一直让我失望。

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.

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

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

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).

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

Hopefully I've explained my problem plainly, if you would require any additional info please let me know.

希望我已经清楚地解释了我的问题,如果您需要任何其他信息,请告诉我。

Also I will be AFK for another 4 hours, so my reply might be a little bit delay.

另外我会再AFK 4个小时,所以我的回复可能会有点延迟。

Thank you in advance for any help!

预先感谢您的任何帮助!

回答by Tim Williams

You can look at the Scripting.Dictionaryobject: it allows you to associate values with string "keys"

您可以查看Scripting.Dictionary对象:它允许您将值与字符串“键”相关联

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

回答by dasg7

Good idea Tim W.

好主意蒂姆W。

With a little modification. Try running this one:

稍加修改。尝试运行这个:

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