VBA: Delete all rows that contain certain values in a specific column

Remi909

New Member
Joined
Mar 22, 2022
Messages
29
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
Hi,

Im trying to create a VBA code that would loop through Column B to check if:

If cells in column B have either "Printer" or "N/A" to delete the entire row. There are about 10,000 rows in column B of which 5833 rows at random have either "Printer" or "N/A". Here's the code I currently have but I keep getting an error "Run time error '13' Type mismatch" when ran.

VBA Code:
Sub Clean_Data()

    Dim WB As Workbook
    Dim WSR As Worksheet
    Dim I As Long
    Dim Rng As Range, Cell As Range

    Set WB = ActiveWorkbook
    Set WSR = WB.Worksheets("Equip")
    Set Rng = Range(Range("B2"), Range("B" & Rows.Count).End(xlUp))

For Each Cell In Rng
    
    If Cell = "Printer" or Cell = "N/A" Then  
        Cell.EntireRow.Delete
    End If

Next Cell

End Sub

Please help. Thank you!
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Are the values of "N/A" in the sheet literal text values of "N/A", or is it the "#N/A" error returned in some Excel formulas?
 
Upvote 0
The below is just correcting the issue you are having.
When you delete rows you need to start from the bottom and work your way up. If you work top down then everytime you delete a row the next row you are about to reference moves up and throws out your referencing.

I think this will run far too slow on 10k records and also if it doesn't delete N/A you will need to address Joe's question.

VBA Code:
Sub Clean_Data_mod()

    Dim WB As Workbook
    Dim WSR As Worksheet
    Dim I As Long
    Dim Rng As Range, Cell As Range

    Set WB = ActiveWorkbook
    Set WSR = WB.Worksheets("Equip")
    Set Rng = Range(Range("B2"), Range("B" & Rows.Count).End(xlUp))

    For I = Rng.Count To 1 Step -1
        If Rng.Cells(I) = "Printer" Or Rng.Cells(I) = "N/A" Then
            Rng.Cells(I).EntireRow.Delete
        End If    
    Next I

End Sub
 
Upvote 0
A few things to note, that I saw in your original code, and looks like Alex may have unintentionally carried down.

You are setting Workbook and Worksheet variables, but neither are referenced in setting the "Rng" range.
So, that range will be set to whatever is the active sheet at the time the code runs. If you meant to include Workbook and Worksheet references in that range, you will need to add them in.

Also, you can speed up the current code by suppressing calculations and screen updating while the code is running, i.e. put this before your "For" loop code:
VBA Code:
    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual
and then by putting this after the loop ends (after the "Next I" line):
VBA Code:
    Application.Calculation = xlCalculationAutomatic
    Application.ScreenUpdating = True
 
Upvote 0
Are the values of "N/A" in the sheet literal text values of "N/A", or is it the "#N/A" error returned in some Excel formulas?
Apologies I missed the #. Its "#N/A"
 
Upvote 0
Apologies I missed the #. Its "#N/A"
It is using preferrable to correct errors rather than deal with them.
What is the formula in those cells that returns that error?
 
Upvote 0
It is using preferrable to correct errors rather than deal with them.
What is the formula in those cells that returns that error?

The sheet in question was copied from a master sheet. I've tried checking what the original formula was but only shows "#N/A". I just need to clean the copied sheet and keep the data that's relevant.
 
Upvote 0
I suggest you go with whatever Joe advises but since I am login off for the night, this is where I got to.

VBA Code:
Sub Clean_Data_mod()

    Dim WB As Workbook
    Dim WSR As Worksheet
    Dim I As Long
    Dim Rng As Range
  
    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual

    Set WB = ActiveWorkbook
    Set WSR = WB.Worksheets("Equip")
    With WSR
        Set Rng = .Range(.Range("B2"), .Range("B" & .Rows.Count).End(xlUp))
    End With

    For I = Rng.Count To 1 Step -1
        If Rng.Cells(I).Text = "#N/A" Then
             Rng.Cells(I).EntireRow.Delete
        ElseIf Rng.Cells(I) = "Printer" Then
            Debug.Print Rng.Cells(I).Address
            Rng.Cells(I).EntireRow.Delete
        End If
    Next I
  
    Application.Calculation = xlCalculationAutomatic
    Application.ScreenUpdating = True

End Sub
 
Upvote 0
Solution
The sheet in question was copied from a master sheet. I've tried checking what the original formula was but only shows "#N/A". I just need to clean the copied sheet and keep the data that's relevant.
OK, if the value is now hard-coded, try this slight variation to Alex's code:
VBA Code:
Sub Clean_Data_mod()

    Dim WB As Workbook
    Dim WSR As Worksheet
    Dim I As Long
    Dim Rng As Range
  
    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual

    Set WB = ActiveWorkbook
    Set WSR = WB.Worksheets("Equip")
    With WSR
        Set Rng = .Range(.Range("B2"), .Range("B" & .Rows.Count).End(xlUp))
    End With

    For I = Rng.Count To 1 Step -1
        If Rng.Cells(I) = "#N/A" Or Rng.Cells(I) = "Printer" Then
            Rng.Cells(I).EntireRow.Delete
        End If
    Next I
  
    Application.Calculation = xlCalculationAutomatic
    Application.ScreenUpdating = True

End Sub
 
Upvote 0
@Joe4, I had tried that and if I type #N/A into a cell and then run that code it error out on that line with Type 13 mismatch error.
That is why I ended up testing for that in a line on its own.
 
Upvote 0

Forum statistics

Threads
1,214,973
Messages
6,122,534
Members
449,088
Latest member
RandomExceller01

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