1
我尝试使用下面的代码从现有的2维数组填充了独特的键与值的最后一个数组VBA填充数组: 最终阵列有3个维度的数据,我想数组看看像这样:从另一个阵列
Finalarray(0):{A,1,4,8} .... Finalarray(4):{E,空,空,12}
我的代码初始化上面与阵列关键字即a,b,c,d,e但是,我不确定什么是最好的一般填充方式!
假设每个“一”的条目是新一行
我的尝试,目前的工作(但很手动)这只是挑出来的“e”:
Sub ArrayTest()
Dim PreservedKeys As Variant
Dim Data(0 To 2, 0 To 3) As Variant
Dim rRef As Range
Dim PreservedData As Variant
Dim MergedArray As Variant
Dim i As Integer
Dim uniquePreservedKeys As Variant
Dim FinalArray
Dim Constant As Integer
PreservedKeys = Array("a", "b", "c", "a", "b", "c", "d", "a", "b", "c", "d", "e")
PreservedData = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
Constant = 3
ReDim MergedArray(0 To UBound(PreservedKeys), 0 To 1)
For i = 0 To UBound(PreservedKeys)
MergedArray(i, 0) = PreservedKeys(i)
MergedArray(i, 1) = PreservedData(i)
Next i
uniquePreservedKeys = M_snb(PreservedKeys)
ReDim FinalArray(0 To UBound(uniquePreservedKeys), 0 To Constant)
For i = 0 To 4
FinalArray(i, 0) = uniquePreservedKeys(i)
Next i
Set rRef = Application.Range("TestRange")
rRef.Resize(UBound(Data, 1) + 1, UBound(Data, 2) + 1) = Data
'MY ATTEMPT SO FAR --> Very manual to just get the e entry
If MergedArray(i, 0) = "a" Then
counter = counter + 1
End If
If counter = 1 Then
If MergedArray(i, 0) <> "e" Then
FinalArray(4, counter) = ""
Else
FinalArray(4, counter) = MergedArray(i, 1)
End If
End If
If counter = 3 Then
If MergedArray(i, 0) <> "e" Then
FinalArray(4, counter) = ""
Else
FinalArray(4, counter) = MergedArray(i, 1)
End If
End If
Next i
End Sub
Function M_snb(UniqueKeys As Variant)
With CreateObject("scripting.dictionary")
For Each it In UniqueKeys
c10 = .Item(it)
Next
an = .keys ' the array .keys contains all unique keys
End With
M_snb = an
End Function
你是否试图在每一行中找到没有重复的单元格,并确定它们的列号? –
最终我想要A,B,CD,E为列和条目为行之下,但这样做,我需要输出Finalarray(0):{A,1,4,8} .... Finalarray(4):首先是{e,Empty,Empty,12}数组。 – user8608110