VBA Excel,循环变量
我试着在下面修复了几个小时,我想我只是被它永久地卡住了。切中要害 当前代码:VBA Excel,循环变量,excel,vba,loops,Excel,Vba,Loops,我试着在下面修复了几个小时,我想我只是被它永久地卡住了。切中要害 当前代码: 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
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
我希望能够以与返回type:TypeName()相同的方式返回变量名。所以对于第一个循环,msgbox会说:i1等等。我尝试了不同的组合。名称等,但似乎失败了我所有的时间
问题2
问题1的原因是,作为下一步,我想根据变量名向原始值添加一些内容
希望我能有这样的东西(显然这段代码不起作用,它只是用于演示):
其中iSomeNumber将来自程序的另一部分,然后我可以稍后检索更新的单个变量(即i1将不再是值1,而是1+iSomeNumber)
希望我已经清楚地解释了我的问题,如果您需要任何其他信息,请让我知道
此外,我将在AFK的另外4个小时,所以我的答复可能会有点延迟
提前感谢您的帮助 您可以查看
Scripting.Dictionary
对象:它允许您将值与字符串“键”相关联
好主意,蒂姆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
谢谢你,蒂姆,非常圆滑的方法!:)
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
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