0

假设我确实有一个数组 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

编辑1

      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) 项目的子详细信息需要收集在一维数组中

更新

      strJoin = ","
For ChildKey In ChildDic.Keys

    strJoin=Join(ChildDic(ChildKey),",") & strJoin

Next

谢谢

4

1 回答 1

5

我们可以用一条语句将字典项(数组)放入 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

输出:

在此处输入图像描述

于 2012-12-23T10:33:35.847 回答