我们可以用一个语句把字典项目(数组)放到一个Range中吗?
假设我有一个数组A1(6)=(45,25,36,88),A2(6)=(14,25,11),A3(6)=(11,21,20,25,48 )。现在,我们可以将这些数组值放在单个数组的单个语句的帮助下,如在这里所有的行都放在一个Excel的范围内,在这里说“C1:R3”范围。
Dim R R = Split(Join(A1, ",") & "," & Join(A2, ",") & "," & Join(A3, ","), ",") Range("C5:T5").Value = R
现在我们可以做字典项目(这是一个数组)结合成一维数组,并分配给一个范围?
For Each ChilID In ChildIDs Redim ChildDetailArray(ArrIndex) ChildMatchNum=objExcel1.Application.WorksheetFunction.Match(ChilID, ob3.Columns(1), 0) ChildDetailArray=ob1.Range(ob1.Cells(ChildMatchNum,1),ob1.Cells(ChildMatchNum,ArrIndex+1)).Value ChildDic.Add ChilID,ChildDetailArray '(ChildDetailArray is an array) Next
EDIT1
suppose a process#20 has 2 child processes say #12,#13. now i used a dictionary object Dic Dic(12)=Arr(10,11,,,18) 'child details Dic(13)=Arr(5,8,9,,,) ' child details ***Output:*** `1D array say ArrMerger()=(10,11,,,18,5,8,9,,,)`
上面的For Loop
做的是相同的。现在,当Loop
完成时,我希望Dic(12)和Dic(13)的项目需要收集在一维数组中
UPDATE
strJoin = "," For ChildKey In ChildDic.Keys strJoin=Join(ChildDic(ChildKey),",") & strJoin Next
谢谢
我们可以把单个语句的字典项(数组)放入一个Range吗? 是的,你可以把所有的字典项目放到一个范围内。
试试这个代码,并解释清楚 /评论你需要的任何变化:
码:
Option Explicit Sub getMerged1DItems() Dim d As Object, d2 As Object Dim vArr As Variant Dim vArr2 As Variant Dim strJoin As String Set d = CreateObject("Scripting.Dictionary") Set d2 = CreateObject("Scripting.Dictionary") '-- assume you have items in your dictionary d.Add "Names", 1 d.Add "Titles", 2 d.Add "Jobs", 3 d.Add "Education", 4 d.Add "Experience", 5 '-- add dictionary items into an 1D array vArr = d.Keys '-- add 1D arryas into d2 dictionary as items d2("v" & 1) = vArr d2("v" & 2) = vArr '-- join multiple 1D array items into one string delimitted by comma strJoin = Join(d2("v" & 1), ", ") & "," & Join(d2("v" & 2), ", ") '-- split the string by comma delimiter vArr2 = Split(strJoin, ",") '-- output to sheet using first 1D Array Sheets(1).Range("B2").Resize(1, _ UBound(Application.Transpose(vArr))) = vArr '-- output to sheet using dictionary Sheets(1).Range("B4").Resize(1, _ UBound(Application.Transpose(d.Keys))) = d.Keys 'output to sheet using mergeed 1D array Sheets(1).Range("B7").Resize(1, _ UBound(Application.Transpose(vArr2))) = vArr2 Set d2 = Nothing Set d = Nothing End Sub
输出: