VBA Macro to delete row based on data value

TheCobbler

New Member
Joined
Aug 21, 2021
Messages
49
Office Version
  1. 365
Platform
  1. Windows
Hi, I'm looking for some help with a VBA macro I have.

I'm trying to delete any row that doesn't start with a 1 or 2 in column A.
This data changes daily so the amount of rows varies.

The code I have below only seems to do what I need on a sporadic basis and after breaking it down for hours I'm still unable to get it to work... I have also tried other method which have also failed! Please point me in the correct direction if there's a better way.

Thanks in advance, C

Example Data:
capture 2.PNG


Current cribbed code - admittedly it may be a tad advanced for me at the moment.

VBA Code:
Sub TrckMac ()

    Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("Sheet1")
    
    Dim SearchRange As Range, SearchCell As Range, DeleteMe As Range
    
    Set SearchRange = ws.Range("A2:A" & ws.Range("A" & ws.Rows.Count).End(xlUp).Row)
    
    For Each SearchCell In SearchRange
        If Left(SearchCell, 1) = 6 Or Left(SearchCell, 1) = "m" Then
            If DeleteMe Is Nothing Then
                Set DeleteMe = SearchCell
            Else
                Set DeleteMe = Union(DeleteMe, SearchCell)
            End If
        End If
    Next SearchCell
    
    If Not DeleteMe Is Nothing Then DeleteMe.EntireRow.Delete

End Sub
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.

Forum statistics

Threads
1,223,908
Messages
6,175,306
Members
452,633
Latest member
DougMo

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