+ Reply to Thread
Results 1 to 6 of 6

VBA to MATCH cells and Delete contents!

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    02-09-2015
    Location
    malta
    MS-Off Ver
    Microsoft 365
    Posts
    383

    VBA to MATCH cells and Delete contents!

    Hi All,

    If it is not too much of an inconvenience to ask, would it be possible, if someone can asist me with the attached file.

    Basically, the file is an extract of a large database and contains 2 sheets.

    In Sheet 1, column A, there are 4 usernames: 'DAVBOR', 'KEIBRI', 'ALBCIL' and 'ANDGAL'. In sheet 1, these usernames will be unique.
    In Sheet 2, column A, there will be a whole column full of these usernames. In sheet 2, there can be more than one occurence of the same username.

    I would like to create a macro/VBA which would look in Sheet 2, Column A and:
    - find those usernames which are present in Sheet 2 but not in Sheet 1,
    - delete all those usernames- just found - from Sheet 2(i.e. delete those usernames found in Sheet 2 but not in Sheet 1)

    Apprciate a lot your kind help!


    Thanks a lot!

    Keibri
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    12-15-2009
    Location
    Chicago, IL
    MS-Off Ver
    Microsoft Office 365
    Posts
    3,177

    Re: VBA to MATCH cells and Delete contents!

    Give this code a try
    Sub DeleteRecords()
    On Error Resume Next
    Dim ws As Worksheet
    Dim LR As Long
    
    Set ws = ThisWorkbook.Worksheets("Sheet2")
    
    With ws
    LR = .Cells(Rows.Count, 1).End(xlUp).Row
    
        .Range("B:B").ClearContents
        
        With .Range("B1:B" & LR)
            .Formula = "=IF(ISERROR(MATCH(A1, Sheet1!A:A, 0)), ERROR.TYPE(1), """")"
            .SpecialCells(xlCellTypeFormulas, xlErrors).EntireRow.Delete
        End With
    
    End With
    
    Set ws = Nothing
    End Sub

  3. #3
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: VBA to MATCH cells and Delete contents!

    Maybe:

    Sub Keibri()
    Dim i As Long, x As Long, y As Range, z
    x = Sheets("Sheet2").Range("A" & Rows.Count).End(3).row
    ReDim z(2 To x)
    With Sheets("Sheet2")
        For i = LBound(z) To UBound(z)
            Set y = Sheets("Sheet1").Columns(1).Find(.Cells(i, "A"), LookIn:=xlValues, lookat:=xlWhole)
                If Not y Is Nothing Then
                    GoTo zz
                Else
                    .Cells(i, "A").Delete xlUp
                End If
            Set y = Nothing
    zz:
        Next i
    End With
    
    End Sub

  4. #4
    Forum Contributor
    Join Date
    02-09-2015
    Location
    malta
    MS-Off Ver
    Microsoft 365
    Posts
    383

    Re: VBA to MATCH cells and Delete contents!

    Cheers guys !!! Thanks a lot for your hep. tested both solutions!

    John, the only small issue I had, that when the VBA is run it will delete only the first occurrence. In the sense that if a username, which is not present in sheet 1, is repeated more than once in sheet 2, the macro will delete only the first occurrence. As for the rest, it worked great!! Thanks a lot for your help!

  5. #5
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: VBA to MATCH cells and Delete contents!

    You're welcome. Glad to help out and thanks for the feedback. Modified.

    Sub Keibri()
    Dim i As Long, x As Long, y As Range, z, xx As Long, ws1 As Worksheet, ws2 As Worksheet
    Set ws1 = Sheets("Sheet1")
    Set ws2 = Sheets("Sheet2")
    x = ws2.Range("A" & Rows.Count).End(3).row
    ReDim z(2 To x)
    With ws2
    xx = ws1.Range("A" & Rows.Count).End(3).row
        For i = LBound(z) To UBound(z)
            For Each y In ws1.Range(ws1.Cells(2, "A"), ws1.Cells(xx, "A"))
                If y.Value = .Cells(i, "A") Then
                    GoTo zz
                Else
                    .Cells(i, "A").Delete xlUp
                End If
            Next y
    zz:
        Next i
    End With
    End Sub

  6. #6
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,769

    Re: VBA to MATCH cells and Delete contents!

    Really not needed as OP has got two working solutions but one could also use autofilter with array:

    Sub DelRows()
    Dim MyArray As Variant
    Dim i As Long
    
    i = Range("A" & Rows.Count).End(xlUp).Row
    
    Sheets("Sheet1").Range("A1:A" & Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row).Name = "aList"
    
    MyArray = Split(Join(Application.Transpose(Range("aList")), Chr(34)), Chr(34))
    
    Range("A1").CurrentRegion.AutoFilter Field:=1, Criteria1:=MyArray, Operator:=xlFilterValues
    
    ActiveSheet.AutoFilter.Range.SpecialCells(xlCellTypeVisible).Copy Range("A" & i + 2)
    
    ActiveSheet.AutoFilterMode = False
    
    Rows(1 & ":" & i + 1).EntireRow.Delete
    
    End Sub
    Alf
    Last edited by Alf; 11-23-2016 at 10:21 AM.

+ 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. Delete only some of a cells contents
    By Dendrinos2 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 08-11-2014, 06:16 AM
  2. Replies: 6
    Last Post: 12-07-2012, 05:38 PM
  3. [SOLVED] Delete contents of blank cells
    By freud1 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-30-2012, 10:53 AM
  4. Delete Contents of Cells with 0
    By EJensen in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-04-2009, 05:10 PM
  5. Delete contents of cells
    By mikespeck in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 07-27-2006, 10:25 AM
  6. [SOLVED] Delete Contents of Cells
    By ir26121973 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 04-05-2006, 02:15 PM
  7. Delete specific cells contents in a row with some locked cells in the same row
    By trussman in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-01-2005, 03:06 PM

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