我如何将一个数组中的值分配给另一个数组中的变量

问题描述 投票:0回答:1

我正在尝试将1分配给变量firstCol,将2分配给变量secondCol ...

[当我开始循环通过colHeaders时,循环变量Header不具有任何值。我知道语法不正确,并且为Header指定了num,但我认为这是解释我要执行的操作的最佳方法。

是否可以遍历变量?

Dim colHeaders() As Variant
Dim Dict As New Scripting.Dictionary
Set Dict = New Scripting.Dictionary

colHeaders = Array(firstCol, secondCol, thirdCol, fourthCol, fifthCol)

num = 1
For Each Header In colHeaders
    Header = num
    num = num + 1
Next Header
vba excel-vba
1个回答
0
投票
Dim Dict As New Scripting.Dictionary
Set Dict = New Scripting.Dictionary

Dim colHeaders() As Variant
colHeaders = Array("firstCol", "secondCol", "thirdCol", "fourthCol", "fifthCol")

Dim num As Long
num = 1

Dim header As Variant
For Each header In colHeaders
    Dict.Add header, num
    num = num + 1
Next header

Debug.Print Dict("secondCol")
© www.soinside.com 2019 - 2024. All rights reserved.