我们可以用一个语句将字典项(数组)放入一个范围吗? [英] Can we put dictionary items(array) into a Range with a single statement?

查看:115
本文介绍了我们可以用一个语句将字典项(数组)放入一个范围吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我确实有一个数组A1(6)=(45,25,,36,88),A2(6)=(14,25,11),A3(6)=(11,21,20, 25,48).现在,我们可以借助单个语句(如将单个数组分配给一行)将这些数组值放入数组,因为此处所有行都位于Excel的范围内,在这里说"C1:R3"范围.

Suppose i do have an array A1(6)=(45,25,,36,88),A2(6)=(14,25,11),A3(6)=(11,21,20,25,48).Now can we put those array values with the help of a single statement like single array assignment to a row,as here all the rows to a range of an Excel, Say here "C1:R3" range.

Dim R
R = Split(Join(A1, ",") & "," & Join(A2, ",") & "," & Join(A3, ","), ",")
Range("C5:T5").Value = R

现在我们可以将字典项(数组)组合成一维数组并分配回范围吗?

Now can we do dictionary Items(which is an array) combine into a 1D array and assign back to a Range?

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

The above For Loop is doing the same.now when the Loop will be finished,i want those child details which are the item of Dic(12) and Dic(13) needs to be collected in an 1D array

UPDATE

UPDATE

      strJoin = ","
For ChildKey In ChildDic.Keys

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

Next

谢谢

推荐答案

我们可以用单个语句将字典项(数组)放入范围吗? 是的,您可以得到所有将字典项归入一个范围.

尝试使用此代码并明确说明 /注释您需要的任何更改:

Try this code and explain clearly / comment any changes you require:

代码:

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

输出:

这篇关于我们可以用一个语句将字典项(数组)放入一个范围吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆