Macro That Deletes Rows Based On Cell Values

bruntonomo

Board Regular
Joined
Jul 29, 2018
Messages
67
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I have looked at some previous threads on the message board that are similar to mine, but my particular case has a bit of a hang up that I would like to post a thread for.

I would like to create a macro to run that will delete rows that have particular names of people in the rows. Ideally I would like to be able to create a list of names in the macro so that it can just remove all desired names each time I run this report. Data is generated by a system that separates the first and last names which makes things a bit more complicated. This particular point is why I'm asking on a separate thread.

Attached is a screen shot of a sample report that I would be using the macro on. Can someone please help with this?
 

Attachments

  • ReportFormat.PNG
    ReportFormat.PNG
    14.7 KB · Views: 14

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Will the names change every time you run the macro? If so, it would be best to make a list of names that you want to delete on another sheet and have the macro refer to that list. This way you don't have to modify the macro, bit instead modify the list. The list could also be with two columns, one for first name and one for last name. Would this work for you?
 
Upvote 0
Will the names change every time you run the macro? If so, it would be best to make a list of names that you want to delete on another sheet and have the macro refer to that list. This way you don't have to modify the macro, bit instead modify the list. The list could also be with two columns, one for first name and one for last name. Would this work for yo
The names to be deleted? Those names should stay the same most of the time. There will be times people are replaced, but for the most part, it should be the same. I would be fine with having the macro reference another sheet.
 
Upvote 0
What is your response?
 
Upvote 0
What is your response?
The names to be deleted? Those names should stay the same most of the time. There will be times people are replaced, but for the most part, it should be the same. I would be fine with having the macro reference another sheet.

Sorry, the network here is experiencing problems. It dropped out on me and caused a blank response somehow.
 
Upvote 0
Start by naming a blank sheet as "Names". Enter the headers First Name and Last Name in columns A and B and fill in the data for the names you want to delete starting in row 2. You can change this list as needed and the macro will still work. Change the sheet names (in red) to suit your needs.
Rich (BB code):
Sub DeleteRows()
    Application.ScreenUpdating = False
    Dim lRow As Long, srcWS As Worksheet, desWS As Worksheet, i As Long, v1 As Variant, v2 As Variant, dic As Object, Val As String
    Set desWS = Sheets("Data")
    Set srcWS = Sheets("Names")
    With desWS
        lRow = .Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
        .Columns("A:A").Insert Shift:=xlToRight
        .Range("A2:A" & lRow).Formula = "=B2 & C2"
    End With
    v1 = desWS.Range("B2", desWS.Range("B" & Rows.Count).End(xlUp)).Resize(, 2).Value
    v2 = srcWS.Range("A2", srcWS.Range("A" & Rows.Count).End(xlUp)).Resize(, 2).Value
    Set dic = CreateObject("Scripting.Dictionary")
    For i = 1 To UBound(v1, 1)
        Val = v1(i, 1) & v1(i, 2)
        If Not dic.Exists(Val) Then
            dic.Add Val, Nothing
        End If
    Next i
    For i = 1 To UBound(v2, 1)
        Val = v2(i, 1) & v2(i, 2)
        If dic.Exists(Val) Then
            With desWS
                .Range("A1").CurrentRegion.AutoFilter 1, Val
                .AutoFilter.Range.Offset(1).EntireRow.Delete
            End With
        End If
    Next i
    With desWS
        .AutoFilterMode = False
        .Columns(1).Delete
    End With
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
Wow! That is fantastic! Thank you so much for the help! This will save me quite a bit of time each morning.
 
Upvote 0
You are very welcome. :)
 
Upvote 0
I've been trying to get a macro like this to work for a while now with no luck, either the code won't run (but without erroring), or I get some kind of error.

With this code I get a 429 "Active X can't create object" error in the line:
Set dic = CreateObject("Scripting.Dictionary")

Any ideas? I'm running the latest version of Excel on a Mac.
 
Upvote 0

Forum statistics

Threads
1,213,511
Messages
6,114,054
Members
448,543
Latest member
MartinLarkin

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top