我是使用字典的新手,可以提供一些帮助。我有一个A1范围内的数据表:C4
A B C. 1 4 7 2 5 8 3 6 9
有没有办法将这些表直接添加到字典中?
提前致谢
答案 0 :(得分:2)
Dim d As Scripting.Dictionary
Dim r As Excel.Range
Dim c As Excel.Range
Set d = New Scripting.Dictionary
Set r = Range("a1:c4")
For Each c In r.Cells
d.Add CStr(c.Address), c.Value
Next c
答案 1 :(得分:2)
我认为您可能正在寻找的是Multidimensional Array
标准Array
将在列表中包含一系列值,并且可以引用此列表中任何点的值,例如:
myArray = Array("One", "Two", "Three")
'The first value in an array is at position 0 unless otherwise specified
MsgBox myArray(0) 'Will open a message box with the value "One"
MsgBox myArray(1) 'Will open a message box with the value "Two"
MsgBox myArray(2) 'Will open a message box with the value "Three"
标准数组是一维的,使用Multidimensional Array
可以向此列表添加多个维度。简单地说,二维数组就可以让你创建一个数据表。
dim myArray(1 to 3, 1 to 3) as Variant
将创建一个二维数组,通过指定'1到3'将分配一组可以在数组中引用的项目大小和范围。以此表为例:
A B C
d电子网
G H I
将它放入多维数组中将是以下
Dim myArray(1 To 3, 1 To 3) As Variant
myArray(1, 1) = "A"
myArray(1, 2) = "B"
myArray(1, 3) = "C"
myArray(2, 1) = "D"
myArray(2, 2) = "E"
myArray(2, 3) = "F"
myArray(3, 1) = "G"
myArray(3, 2) = "H"
myArray(3, 3) = "I"
MsgBox myArray(2, 2) 'Will open a message box with the value "E"
鉴于您希望从Range(“A1:C4”)生成此项,您可以使用循环遍历每个单元格来创建它:
Dim myArray(1 To 4, 1 To 3) As Variant
For Each c In Range("A1:C4")
myArray(c.Row, c.Column) = c.Value
Next c