+ Reply to Thread
Results 1 to 8 of 8

Difference between two 2D Arrays

Hybrid View

  1. #1
    Registered User
    Join Date
    11-23-2020
    Location
    Kyoto
    MS-Off Ver
    Microsoft Office Standard 2013
    Posts
    6

    Difference between two 2D Arrays

    I am trying to get a difference of two arrays as a third array.

    Here is a sample of what I am trying to do:

    sample.png

    So far I got this:

    Sub Test()
    
    Dim arr1 As Variant
    Dim arr2 As Variant
    Dim arr3 As Variant
    Dim coll As Collection
    Dim i As Long, j As Long
    
    With Worksheets("Sheet2")
        LastRowColumnA = .Cells(.Rows.Count, 1).End(xlUp).Row
        arr1 = .Range("A1:C" & LastRowColumnA).Value
    End With
    
    With Worksheets("Sheet1")
        LastRowColumnA = .Cells(.Rows.Count, 1).End(xlUp).Row
        arr2 = .Range("A1:C" & LastRowColumnA).Value
    End With
    Set coll = New Collection
    For i = LBound(arr1, 1) To UBound(arr1, 1)
        For j = LBound(arr1, 2) To UBound(arr1, 2)
            coll.Add arr1(i, j), arr1(i, j)
        Next j
    Next i
    
    For i = LBound(arr2, 1) To UBound(arr2, 1)
        For j = LBound(arr2, 2) To UBound(arr2, 2)
            On Error Resume Next
            coll.Add arr2(i, j), arr2(i, j)
            If Err.Number <> 0 Then
                coll.Remove arr2(i, j)
            End If
            On Error GoTo 0
       Next j
    Next i
    
    ReDim arr3(1 To coll.Count, 1 To 1)
    
    For i = 1 To coll.Count
        arr3(i, 1) = coll(i)
        Debug.Print arr3(i, 1)
    Next i
    
    Worksheets("Sheet2").Range("F1").Resize(UBound(arr3, 1), 1).Value = arr3
    End Sub
    But I am getting a type mismatch error on this line:
    coll.Add arr1(i, j), arr1(i, j)
    Any one know what can be wrong? Or even if I am correct with the code so far?
    Attached Files Attached Files
    Last edited by Pimo; 11-24-2020 at 01:01 AM. Reason: solved

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    26,969

    Re: Difference between two 2D Arrays

    In your example, why does your Output show both mismatching records for toy4 but only one record for toy1?

    Is your sample data in your picture the same as what you are running the code on?

    When the error occurs, hover the cursor over arr1(i, j). What is the value?

  3. #3
    Registered User
    Join Date
    11-23-2020
    Location
    Kyoto
    MS-Off Ver
    Microsoft Office Standard 2013
    Posts
    6

    Re: Difference between two 2D Arrays

    --6StringJazzer so I added sample workbook, hope that it will be more clear


    --Adam, Can you tell me arr1() is empty? I can't get it because code for the array seems to be fine
    Also I have no idea what you mean with not correct syntax, Do you suggest that it should looks more like this?
    coll.Add item := arr1(i, j), key := arr1(i, j)
    Last edited by Pimo; 11-23-2020 at 10:33 PM. Reason: to not post 2 posts

  4. #4
    Banned User!
    Join Date
    02-06-2020
    Location
    Iowa City, IA, USA
    MS-Off Ver
    2016 - 365 / 2007
    Posts
    2,014

    Re: Difference between two 2D Arrays

    you are getting the err for 2 reasons I believe. first of all, there are no elements in the array arr1(). second of all, according to this:

    https://docs.microsoft.com/en-us/off...r-applications

    you do not have the correct syntax of the method:
    MyClasses. Add item := Inst, key := CStr(Num)

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

    Re: Difference between two 2D Arrays

    This is to amend your original code.
    Sub test()
    
    Dim arr1 As Variant
    Dim arr2 As Variant
    Dim arr3 As Variant
    Dim coll As Collection
    Dim i As Long, j As Long, ii As Long, txt As String, x
    
    With Worksheets("Sheet2")
        LastRowColumnA = .Cells(.Rows.Count, 1).End(xlUp).Row
        arr1 = .Range("A1:C" & LastRowColumnA).Value
    End With
    
    With Worksheets("Sheet1")
        LastRowColumnA = .Cells(.Rows.Count, 1).End(xlUp).Row
        arr2 = .Range("A1:C" & LastRowColumnA).Value
    End With
    Set coll = New Collection
    On Error Resume Next
    For i = LBound(arr1, 1) To UBound(arr1, 1)
        txt = Join(Array(arr1(i, 1), arr1(i, 2), arr1(i, 3)), Chr(2))
        coll.Add txt, txt
    Next i
    
    For i = LBound(arr2, 1) To UBound(arr2, 1)
        txt = Join(Array(arr2(i, 1), arr2(i, 2), arr2(i, 3)), Chr(2))
        Err.Clear
        coll.Add txt, txt
        If Err.Number <> 0 Then coll.Remove txt
    Next i
    
    ReDim arr3(1 To coll.Count, 1 To 3)
    
    For i = 1 To coll.Count
        x = Split(coll(i), Chr(2))
        For ii = 0 To 2
            arr3(i, ii + 1) = x(ii)
        Next
    Next i
    
    Worksheets("Sheet2").Range("F1").Resize(UBound(arr3, 1), 3).Value = arr3
    End Sub
    Different method,
    Sub test2()
        Dim ws1 As Worksheet, ws2 As Worksheet, txt As String, temp As String
        Dim e, x, LR, a(1), n As Long
        Set ws1 = Sheets("Sheet1"): Set ws2 = Sheets("Sheet2")
        txt = "match(ws1a1:aLR1&ws1b1:bLR1&ws1c1:cLR1,ws2a1:aLR2&ws2b1:bLR2&ws2c1:cLR1,0)"
        For Each e In Array(Array(ws1, ws2), Array(ws2, ws1))
            LR = e(0).[a1].CurrentRegion.Rows.Count
            temp = txt
            temp = Replace( Replace( temp, "ws1", "'" & e(0).Name & "'!"), "ws2", "'" & e(1).Name & "'!")
            temp = Replace( Replace( temp, "LR1", LR), "LR2", e(1).[a1].CurrentRegion.Rows.Count)
            x = Filter(Evaluate("transpose(if(iserror(" & temp & "),row('" & e(0).Name & "'!a1:a" & LR & ")))"), False, 0)
            a(n) = x: n = n + 1
        Next
        With ws2
            If UBound(a(0)) > -1 Then
                .Cells(n - 1, "f").Resize(UBound(a(0)) + 1, 3) = Application.Index(ws1 _
                .[a1].CurrentRegion.Value, .Application.Transpose(a(0)), Array(1, 2, 3))
                n = n + UBound(a(0)) + 1
            End If
            If UBound(a(1)) > -1 Then
                .Cells(n - 1, "f").Resize(UBound(a(1)) + 1, 3) = Application.Index(ws2 _
                .[a1].CurrentRegion.Value, .Application.Transpose(a(1)), Array(1, 2, 3))
            End If
            .Columns("f").NumberFormat = "0"
        End With
    End Sub

  6. #6
    Registered User
    Join Date
    11-23-2020
    Location
    Kyoto
    MS-Off Ver
    Microsoft Office Standard 2013
    Posts
    6

    Re: Difference between two 2D Arrays

    thanks a lot! both works perfect!
    ありがとうございました!

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

    Re: Difference between two 2D Arrays

    You are welcome.
    どういたしまして。
    コロナには気を付けましょう。京都も大変なようですね。

  8. #8
    Registered User
    Join Date
    11-23-2020
    Location
    Kyoto
    MS-Off Ver
    Microsoft Office Standard 2013
    Posts
    6

    Re: Difference between two 2D Arrays

    気を付けましょう!第3波が来ているみたいですね。
    京都も大変。。。

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Querying Large Series of Arrays And Displaying Unique Values in Arrays [Hard Index Match?]
    By larryg003 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 05-28-2020, 04:52 PM
  2. Replies: 1
    Last Post: 07-29-2014, 08:26 AM
  3. Declaring multiple multi-dimensional arrays (jagged arrays) - compile error?
    By dfribush in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 12-20-2013, 05:06 PM
  4. Get the Difference between Two 2 D Arrays in VBA
    By pavanlalit in forum Excel - New Users/Basics
    Replies: 2
    Last Post: 08-07-2013, 06:03 PM
  5. [SOLVED] New to arrays-where do I find a good beginners guide to multi dimensional arrays
    By mc84excel in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 06-04-2013, 07:44 PM
  6. [SOLVED] Need to count if the difference in values between two arrays is less than 5
    By italianiceman in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 09-07-2012, 03:08 PM
  7. [SOLVED] [SOLVED] VBA - averaging the difference of 2 arrays, ignoring blanks
    By RobPaolillo in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-08-2005, 04:05 PM

Tags for this Thread

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