+ Reply to Thread
Results 1 to 7 of 7

How to compare arrays and delete entire row when condition is met

Hybrid View

  1. #1
    Registered User
    Join Date
    01-06-2013
    Location
    New York, EUA
    MS-Off Ver
    Excel 2013
    Posts
    77

    How to compare arrays and delete entire row when condition is met

    Hi Friends,

    I have a workbook (please, see the attached file sample) with 2 worksheets: "S1" and "S2". Both worksheets have a few rows with 500 cells filled randomly with numbers (1 to 500, no duplicates, columns "B" to "SG"), and column "A" is filled with a number of identification.

    Although each row has 500 cells (columns "B" to "SG"), I need to split the row into 50 groups of 10 cells (columns "B" to "K", columns "L" to "U", and so forth).

    Then, I need to compare each group of 10 cells (50 groups in each row) in worksheets "S2" against each one of the 50 groups with 10 cells in each row in worksheets "S1", and to delete the entire row in worksheet "S2" that has a group of 10 cells with more than 1 common elent when compared to any of the 50 groups in each row in worksheet "S1".

    The final goal is to delete rows in worksheet "S2" that have a group of 10 cells with more than 1 element in common with any of the groups in the rows in worksheet "S1".

    I have written a basic macro with so many loops, that is takes a long time to compare the groups of a single row.

    So, I would like to ask your help to write an advanced code, that could speed up the comparison of each group in worksheet "S2" to each group in worksheet "S1" and delete the entire row in worksheet "S2" if there is a group with more than 1 element in common.

    The sample file shows these two worksheets. The rows with the numbers "102", "104" and "107" in column "A" should be deleted after macro execution, The sample file is similar to the actual file, exept that the actual file has thousands of rows in each worksheet.

    Any help will be appreciated.
    Attached Files Attached Files
    Last edited by JOAO12; 10-01-2020 at 08:53 PM.

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

    Re: How to compare arrays and delete entire row when condition is met

    See if this is how you wanted.
    Sub test()
        Dim s1 As Range, s2 As Range, i As Long, ii As Long
        Dim iii As Long, iv As Long, v As Long, n As Long, x
        Application.ScreenUpdating = False
        Set s1 = Sheets("s1").Cells(1).CurrentRegion
        Set s2 = Sheets("s2").Cells(1).CurrentRegion
        For i = s2.Rows.Count To 1 Step -1
            For ii = 2 To s2.Columns.Count Step 10
                For iii = 1 To s1.Rows.Count
                    x = WorksheetFunction.IfError(Application.Match( _
                        s2.Cells(i, ii).Resize(, 10), s1.Rows(iii), 0), 0)
                    For iv = 2 To s2.Columns.Count Step 10
                        n = 0
                        For v = 1 To 10
                            If (x(v) >= iv) * (x(v) <= iv + 9) Then n = n + 1
                            If n > 1 Then
                                s2.Rows(i).EntireRow.Delete
                                Exit For
                            End If
                        Next
                        If n > 1 Then Exit For
                    Next
                    If n > 1 Then Exit For
                Next
                If n > 1 Then Exit For
            Next
        Next
        Application.ScreenUpdating = True
    End Sub

  3. #3
    Registered User
    Join Date
    01-06-2013
    Location
    New York, EUA
    MS-Off Ver
    Excel 2013
    Posts
    77

    Re: How to compare arrays and delete entire row when condition is met

    Hi Jindon,

    That's it! Thank you very much.

    I would like to ask you one more favor. The previous sample file had worksheets "S1" and "S2" with only one group of 500 cells. Now, worksheets "S1" and "S2" have 4 groups of 500 cells. Could you please adapt your macro?

    The sample file attached has 2 worksheets. In the worksheet "S2", the row with the number "102" in column "A" should be deleted after macro execution (please update "n>1" to "n>4", as this time I would like to delete the rows that have a group of 10 cells with more than 4 elements in common).

    Thanks.
    Attached Files Attached Files

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

    Re: How to compare arrays and delete entire row when condition is met

    I don't understand how you want.

    The first one is comparing every 10 columns of data in a row of S2 with every 10 columns of data in all rows in S1 and delete when it finds more than one match.

    How do you want to compare now?

  5. #5
    Registered User
    Join Date
    01-06-2013
    Location
    New York, EUA
    MS-Off Ver
    Excel 2013
    Posts
    77

    Re: How to compare arrays and delete entire row when condition is met

    Hi Jindon,

    I still need to compare every 10 columns of data in a row.
    Your code compares each one of the 50 groups of 10 cells in columns "B" to "SG" from S2 to:
    - Each one of the 50 groups of 10 cells in columns "B" to "SG" from S1.

    Now, "S1" and "S2" have 4 groups of 500 cells:
    1- Columns "B" to "SG", with 50 groups of 10 cells ("B" to "K", "L" to "U", and so forth);
    2- Columns "SI" to "ALN", with 50 groups of 10 cells ("SI" to "SR", "SS" to "TB", and so forth);
    3- Columns "ALP" to "BEU", with 50 groups of 10 cells ("ALP" to "ALY", "ALZ" to "AMI", and so forth);
    4- Columns "BEW" to "BYB", with 50 groups of 10 cells ("BEW" to "BFF", "BFG" to "BFP", and so forth);

    I need to compare each one of the 50 groups of 10 cells in columns "B" to "SG" from S2 to:
    - Each one of the 50 groups of 10 cells in columns "B" to "SG" from S1;
    - Each one of the 50 groups of 10 cells in columns "SI" to "ALN" from S1;
    - Each one of the 50 groups of 10 cells in columns "ALP" to "BEU" from S1;
    - Each one of the 50 groups of 10 cells in columns "BEW" to "BYB" from S1.

    Then, I need to compare each one of the 50 groups of 10 cells in columns "SI" to "ALN" from S2 to:
    - Each one of the 50 groups of 10 cells in columns "B" to "SG" from S1;
    - Each one of the 50 groups of 10 cells in columns "SI" to "ALN" from S1;
    - Each one of the 50 groups of 10 cells in columns "ALP" to "BEU" from S1;
    - Each one of the 50 groups of 10 cells in columns "BEW" to "BYB" from S1.

    Then, I need to compare each one of the 50 groups of 10 cells in columns "ALP" to "BEU" from S2 to:
    - Each one of the 50 groups of 10 cells in columns "B" to "SG" from S1;
    - Each one of the 50 groups of 10 cells in columns "SI" to "ALN" from S1;
    - Each one of the 50 groups of 10 cells in columns "ALP" to "BEU" from S1;
    - Each one of the 50 groups of 10 cells in columns "BEW" to "BYB" from S1.

    Then, I need to compare each one of the 50 groups of 10 cells in columns "BEW" to "BYB" from S2 to:
    - Each one of the 50 groups of 10 cells in columns "B" to "SG" from S1;
    - Each one of the 50 groups of 10 cells in columns "SI" to "ALN" from S1;
    - Each one of the 50 groups of 10 cells in columns "ALP" to "BEU" from S1;
    - Each one of the 50 groups of 10 cells in columns "BEW" to "BYB" from S1.

    In this new macro, please update "n>1" to "n>4". After running this new macro on "Sample File - 2.xlsx", in the worksheet "S2", the row with the number "102" in column "A" should be deleted.

    Thanks.

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

    Re: How to compare arrays and delete entire row when condition is met

    OOps, so sorry, I didn't realize s1 has more columns up to col.BYB...

    Hope this works as you wanted.
    Sub test()
        Dim s1 As Range, s2 As Range, myAreas As Areas, myArea As Range, r As Range
        Dim i As Long, ii As Long, iii As Long, iv As Long, v As Long, x, n As Long
        Const myStep = 10, myNum = 4
        Application.ScreenUpdating = False
        Set s1 = Sheets("s1").Cells(1).CurrentRegion
        Set s2 = Sheets("s2").Cells(1).CurrentRegion
        For i = 1 To s2.Rows.Count
            For ii = 2 To s2.Columns.Count
                For iii = 1 To s1.Rows.Count
                    Set myAreas = s1.Rows(iii).EntireRow.Resize(, Columns.Count - 1).Offset(, 1).SpecialCells(2, 1).Areas
                    For Each myArea In myAreas
                        x = WorksheetFunction.IfError(Application.Match(s2.Cells(i, ii).Resize(, myStep), myArea, 0), 0)
                        For iv = 1 To myArea.Count Step myStep
                            n = 0
                            For v = 1 To myStep
                                If (x(v) >= iv) * (x(v) <= iv + myStep) Then n = n + 1
                                If n > myNum Then
                                    If r Is Nothing Then
                                        Set r = s2.Rows(i)
                                    Else
                                        Set r = Union(r, s2.Rows(i))
                                    End If
                                    Exit For
                                End If
                            Next
                            If n > myNum Then Exit For
                        Next
                        If n > myNum Then Exit For
                    Next
                    If n > myNum Then Exit For
                Next
                If n > myNum Then Exit For
            Next
        Next
        If Not r Is Nothing Then
            r.EntireRow.Delete
        Else
            MsgBox "No match"
        End If
        Application.ScreenUpdating = True
    End Sub

  7. #7
    Registered User
    Join Date
    01-06-2013
    Location
    New York, EUA
    MS-Off Ver
    Excel 2013
    Posts
    77

    Re: How to compare arrays and delete entire row when condition is met

    Thank you Jindon. You did a great job!

+ 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] delete entire row with condition
    By dumdumbum in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-06-2020, 07:04 AM
  2. [SOLVED] If condition match then delete entire row by vba else do nothing
    By MoldyBread in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 07-10-2020, 10:26 AM
  3. delete empty entire row with condition
    By ash3angel in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 06-29-2015, 08:29 AM
  4. [SOLVED] VBA to delete entire row based on condition met
    By shiva_reshs in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 06-17-2014, 05:48 PM
  5. [SOLVED] Macro to delete entire row on a condition
    By kgonzalbo in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-30-2011, 12:39 AM
  6. Delete entire row on condition
    By bouitac in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-06-2009, 04:58 PM
  7. [SOLVED] Can I delete an entire row if condition is not met?
    By Christine in forum Excel Formulas & Functions
    Replies: 9
    Last Post: 05-04-2006, 04:50 AM

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