VBA - 如何将数组中的每个单元格变成三元组
VBA - How to triplet each cell in the array
我正在尝试获取 A 列中的所有唯一值并将它们插入数组:“uniqueNameList”。
在那之后,我想将我数组中的每个单元格分成三份。
例如:
A 列:
A
One
Two
Three
Three
One
Two
“uniqueNameList”数组将是:
("一", "二", "三")
三连音后为:
("一","一","一","二","二","二","三","三","三")
我试过了,确实得到了预期的“uniqueNameList”数组。
但我没能把物品变成三倍。这是我的代码:
Sub test()
Dim myList As Variant, uniqueNameList As Variant, arr3 As Variant
Dim itemCount As Long
myList = shTest.Range("A1").CurrentRegion
uniqueNameList = WorksheetFunction.Unique(myList)
itemCount = WorksheetFunction.CountA(uniqueNameList)
ReDim arr3(1 To (itemCount * 3))
Dim i As Long, j As Long
For i = 1 To UBound(uniqueNameList)
arr3(i) = uniqueNameList(i, 1)
arr3(i + 1) = uniqueNameList(i, 1)
arr3(i + 2) = uniqueNameList(i, 1)
Next i
End Sub
提前致谢!
您需要将 arr3
的索引乘以 3。对于第一次迭代 (i=1),您想要写入索引 1、2、3,以便进行第二次迭代 (i=2 ) 变成 4、5、6 等等。
For i = 1 To UBound(uniqueNameList)
arr3(i * 3 - 2) = uniqueNameList(i, 1)
arr3(i * 3 - 1) = uniqueNameList(i, 1)
arr3(i * 3 - 0) = uniqueNameList(i, 1)
Next i
如果你的 uniquenamelist 数组从 0 开始,arr3 从 1 开始,那么试试这个。
`For i = LBound(uniqueNameList) To UBound(uniqueNameList)
arr3((3 * i) + 1) = uniqueNameList(i)
arr3((3 * i) + 2) = uniqueNameList(i)
arr3((3 * i) + 3) = uniqueNameList(i)
Next i
`
我正在尝试获取 A 列中的所有唯一值并将它们插入数组:“uniqueNameList”。
在那之后,我想将我数组中的每个单元格分成三份。
例如:
A 列:
A |
---|
One |
Two |
Three |
Three |
One |
Two |
“uniqueNameList”数组将是:
("一", "二", "三")
三连音后为:
("一","一","一","二","二","二","三","三","三")
我试过了,确实得到了预期的“uniqueNameList”数组。 但我没能把物品变成三倍。这是我的代码:
Sub test()
Dim myList As Variant, uniqueNameList As Variant, arr3 As Variant
Dim itemCount As Long
myList = shTest.Range("A1").CurrentRegion
uniqueNameList = WorksheetFunction.Unique(myList)
itemCount = WorksheetFunction.CountA(uniqueNameList)
ReDim arr3(1 To (itemCount * 3))
Dim i As Long, j As Long
For i = 1 To UBound(uniqueNameList)
arr3(i) = uniqueNameList(i, 1)
arr3(i + 1) = uniqueNameList(i, 1)
arr3(i + 2) = uniqueNameList(i, 1)
Next i
End Sub
提前致谢!
您需要将 arr3
的索引乘以 3。对于第一次迭代 (i=1),您想要写入索引 1、2、3,以便进行第二次迭代 (i=2 ) 变成 4、5、6 等等。
For i = 1 To UBound(uniqueNameList)
arr3(i * 3 - 2) = uniqueNameList(i, 1)
arr3(i * 3 - 1) = uniqueNameList(i, 1)
arr3(i * 3 - 0) = uniqueNameList(i, 1)
Next i
如果你的 uniquenamelist 数组从 0 开始,arr3 从 1 开始,那么试试这个。
`For i = LBound(uniqueNameList) To UBound(uniqueNameList)
arr3((3 * i) + 1) = uniqueNameList(i)
arr3((3 * i) + 2) = uniqueNameList(i)
arr3((3 * i) + 3) = uniqueNameList(i)
Next i
`