填充一个二维数组,拆分多行代码
Fill a two-dimensional array, split code on multiple lines
我必须用 8 x 5 = 40 个值填充一个二维数组。
下面的代码有效。
Sub ReadLabelPositions()
Dim Arr_labelpositions() As Variant ' Array 8 rows + 5 columns
' name label, top position, left position, top position2, left position2
Dim Int_Counter1, Int_Counter2 As Integer
' fill array
Arr_labelpositions() = [{"lbl_N",114, 222, 104, 212; "lbl_NO", 144, 144, 134, 154; "lbl_O", 210, 252, 210, 256; "lbl_ZO", 276, 222, 276, 232 ; "lbl_Z",300, 144, 310, 144; "lbl_ZW", 276, 54, 276, 44; "lbl_W", 210, 36, 210, 26; "lbl_NW", 144, 54, 144, 44 }]
'loop through array
For Int_Counter1 = 1 To UBound(Arr_labelpositions, 1)
For Int_Counter2 = 1 To UBound(Arr_labelpositions, 2)
Debug.Print Arr_labelpositions(Int_Counter1, Int_Counter2)
Next Int_Counter2
Next Int_Counter1
End Sub
我想拆分为数组赋值的那一行,因为那一行太长了。
像这样:
Arr_labelpositions() = [{"lbl_N",114, 222, 104, 212; _ <br>
"lbl_NO", 144, 144, 134, 154; _ <br>
"lbl_O", 210, 252, 210, 256; _ <br> etc...
VBA 不支持从静态值创建多维数组。为了实现您的目标,我建议您结合使用集合和数组。集合中的每个项目都将包含一个数组。
Dim myCOlection as Collection
Set myCollection=New COllection
With myCollection
.add Array("lbl_N",114, 222, 104, 212)
.add Array("lbl_NO", 144, 144, 134, 154)
' etc etc.
End with
您现在可以使用语法
引用每个数组中的项目
ThisValue = myCollection(x)(y)
其中 x 是 myCollection 中的项目(实际上是 myCollection.Item(x)),Y 是数组中的索引。
您可能还想看看使用 Scripting.Dictionary 而不是集合是否会给您带来任何优势。
恐怕你不能(以你尝试的方式)。您可以使用数组数组,以下一种方式构建并像这样处理:
Sub testSplitArrayBis()
Dim Arr_labelpositions() As Variant, Int_Counter1 As Long, Int_Counter2 As Long
Arr_labelpositions() = Array(Array("lbl_N", 114, 222, 104, 212), _
Array("lbl_NO", 144, 144, 134, 154), _
Array("lbl_O", 210, 252, 210, 256), _
Array("lbl_ZO", 276, 222, 276, 232), _
Array("lbl_Z", 300, 144, 310, 144), _
Array("lbl_ZW", 276, 54, 276, 44), _
Array("lbl_W", 210, 36, 210, 26), _
Array("lbl_NW", 144, 54, 144, 44))
'loop through array
For Int_Counter1 = 0 To UBound(Arr_labelpositions)
For Int_Counter2 = 0 To UBound(Arr_labelpositions(Int_Counter1))
Debug.Print Arr_labelpositions(Int_Counter1)(Int_Counter2)
Next Int_Counter2
Next Int_Counter1
End Sub
我必须用 8 x 5 = 40 个值填充一个二维数组。
下面的代码有效。
Sub ReadLabelPositions()
Dim Arr_labelpositions() As Variant ' Array 8 rows + 5 columns
' name label, top position, left position, top position2, left position2
Dim Int_Counter1, Int_Counter2 As Integer
' fill array
Arr_labelpositions() = [{"lbl_N",114, 222, 104, 212; "lbl_NO", 144, 144, 134, 154; "lbl_O", 210, 252, 210, 256; "lbl_ZO", 276, 222, 276, 232 ; "lbl_Z",300, 144, 310, 144; "lbl_ZW", 276, 54, 276, 44; "lbl_W", 210, 36, 210, 26; "lbl_NW", 144, 54, 144, 44 }]
'loop through array
For Int_Counter1 = 1 To UBound(Arr_labelpositions, 1)
For Int_Counter2 = 1 To UBound(Arr_labelpositions, 2)
Debug.Print Arr_labelpositions(Int_Counter1, Int_Counter2)
Next Int_Counter2
Next Int_Counter1
End Sub
我想拆分为数组赋值的那一行,因为那一行太长了。
像这样:
Arr_labelpositions() = [{"lbl_N",114, 222, 104, 212; _ <br>
"lbl_NO", 144, 144, 134, 154; _ <br>
"lbl_O", 210, 252, 210, 256; _ <br> etc...
VBA 不支持从静态值创建多维数组。为了实现您的目标,我建议您结合使用集合和数组。集合中的每个项目都将包含一个数组。
Dim myCOlection as Collection
Set myCollection=New COllection
With myCollection
.add Array("lbl_N",114, 222, 104, 212)
.add Array("lbl_NO", 144, 144, 134, 154)
' etc etc.
End with
您现在可以使用语法
引用每个数组中的项目 ThisValue = myCollection(x)(y)
其中 x 是 myCollection 中的项目(实际上是 myCollection.Item(x)),Y 是数组中的索引。
您可能还想看看使用 Scripting.Dictionary 而不是集合是否会给您带来任何优势。
恐怕你不能(以你尝试的方式)。您可以使用数组数组,以下一种方式构建并像这样处理:
Sub testSplitArrayBis()
Dim Arr_labelpositions() As Variant, Int_Counter1 As Long, Int_Counter2 As Long
Arr_labelpositions() = Array(Array("lbl_N", 114, 222, 104, 212), _
Array("lbl_NO", 144, 144, 134, 154), _
Array("lbl_O", 210, 252, 210, 256), _
Array("lbl_ZO", 276, 222, 276, 232), _
Array("lbl_Z", 300, 144, 310, 144), _
Array("lbl_ZW", 276, 54, 276, 44), _
Array("lbl_W", 210, 36, 210, 26), _
Array("lbl_NW", 144, 54, 144, 44))
'loop through array
For Int_Counter1 = 0 To UBound(Arr_labelpositions)
For Int_Counter2 = 0 To UBound(Arr_labelpositions(Int_Counter1))
Debug.Print Arr_labelpositions(Int_Counter1)(Int_Counter2)
Next Int_Counter2
Next Int_Counter1
End Sub