+ Reply to Thread
Results 1 to 7 of 7

Combine Arrays

Hybrid View

  1. #1
    Registered User
    Join Date
    03-08-2005
    Posts
    16

    Combine Arrays

    I have cells that have comma separated data that I need to combine into a single array and then remove duplicates.

    For instance, D2 = "a,b,c", D3 = "b,c,d", D4 = "c,d,e" and I want my final array to show "b,c,d,e".

    Due to other restrictions elsewhere in my code, I need to run a loop to add in the arrays instead of selecting them as a range (the row needs to meet certain criteria to be included).

    Can anyone offer some direction on this? I did copy the functions offered at CPearson, but I am getting errors (http://www.cpearson.com/excel/VBAArrays.htm)

    Thanks in advance.

  2. #2
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,551

    Re: Combine Arrays

    Maybe this

    Sub abc()
     Dim i As Long, ii As Long
     Dim xItem
         
        With CreateObject("scripting.dictionary")
            .comparemode = 1
            For i = 2 To 4
                xItem = Split(Cells(i, "d"), ",")
                For ii = LBound(xItem) To UBound(xItem)
                    If Not .exists(xItem(ii)) Then
                        .Item(xItem(ii)) = xItem(ii)
                    End If
                Next
            Next
            xItem = Join$(.items, ",")
        End With
        MsgBox xItem
    End Sub
    Thanks,
    Mike

    If you are satisfied with the solution(s) provided, please mark your thread as Solved.
    Select Thread Tools-> Mark thread as Solved.

  3. #3
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Combine Arrays

    If you are not Mac, this shoud do it.
    Option Explicit
    
    Sub test()
        Dim r As Range, e
        With CreateObject("Scripting.Dictionary")
            .CompareMode = 1
            For Each r In Range("d2", Range("d" & Rows.Count).End(xlUp))
                If r.Value <> "" Then
                    For Each e In Split(r.Value, ",")
                        .Item(Trim$(e)) = Empty
                    Next
                End If
            Next
            MsgBox Join$(.keys)
        End With
    End Sub

  4. #4
    Registered User
    Join Date
    03-08-2005
    Posts
    16

    Re: Combine Arrays

    These both worked well.

    Thank you so much.

    One further problem. It seems that, although my array now is gathering all of the information, I'm having a type mismatch error (run-time 13) when I'm trying to use it. The error appears at the line with "Unbound(arrStrings)"

    The next piece of code uses each element of the array to match against a cell.

    For i = 0 To UBound(arrStrings)
        Set MARKER = c1
        Do While c1.Value = MARKER.Value And c1.Offset(0, 1).Value = MARKER.Offset(0, 1).Value And c1.Offset(0, 2).Value = MARKER.Offset(0, 2).Value
            If Trim(MARKER.Offset(0, 13).Value) = Trim(arrStrings(i)) Then
                MsgBox ("blah, blah")
            End If
            Set MARKER = MARKER.Offset(1, 0)
        Loop
    Next i
    Anyone got an idea what might be causing this? Went to MSDN developer site, but the information only mentioned a service pack, which I already have.

    Thanks again.

  5. #5
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Combine Arrays

    Quote Originally Posted by kreitzig View Post
    It seems that, although my array now is gathering all of the information, I'm having a type mismatch error (run-time 13) when I'm trying to use it. The error appears at the line with "Unbound(arrStrings)"
    Most probably arrStrins is not an Array...

  6. #6
    Registered User
    Join Date
    03-08-2005
    Posts
    16

    Re: Combine Arrays

    Quote Originally Posted by mike7952 View Post
    Try this instead of using the join
    xItem = .items
    Perfect. Thanks much. It is working good, in fact the whole complicated mess is now working correctly.

    Quote Originally Posted by jindon View Post
    Most probably arrStrins is not an Array...
    Yep, that's what i figured too. I am just a little lost because it's being loaded as an array...but I guess that's the problem.

  7. #7
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,551
    Try this instead of using the join
    xItem = .items

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1