Excel VBA to delete rows that dont match cell value

Stuepef

Board Regular
Joined
Oct 23, 2017
Messages
128
Office Version
  1. 2021
Platform
  1. Windows
I am looking to write a VBA to delete rows based on a call value within Sheet13. If the value in column A (over 50,000 rows) does not match the cell value in H1, then delete that row. With the amount of rows, I am concerned with the speed and efficiency of the code.
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
How about
Code:
Sub FilterDelete()
   Dim Usdrws As Long
   
   Application.ScreenUpdating = False
   Usdrws = Range("A" & Rows.Count).End(xlUp).Row
   Range("A1").AutoFilter 1, "<>" & Range("H1").Value
   Range("A2:A" & Usdrws).SpecialCells(xlVisible).EntireRow.Delete
   Range("A1").AutoFilter
End Sub
 
Upvote 0
I have multiple tabs in the workbook, does Sheet13 have to be incorporated into the code? Also, how would the code look if I moved H1 to a tab named Calculations?
 
Upvote 0
Which sheet is the data on?
 
Upvote 0
The data is on Sheet13. Thinking about moving H1 to "Calculations" tab or Sheet2 code name.
 
Upvote 0
How about
Code:
Sub FilterDelete()
   Dim Usdrws As Long
   
   Application.ScreenUpdating = False
   With sheet13
      Usdrws = .Range("A" & Rows.Count).End(xlUp).Row
      .Range("A1").AutoFilter 1, "<>" & sheet2.Range("H1").Value
      .Range("A2:A" & Usdrws).SpecialCells(xlVisible).EntireRow.Delete
      .Range("A1").AutoFilter
   End With
End Sub
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,215,025
Messages
6,122,734
Members
449,094
Latest member
dsharae57

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