TheCobbler
New Member
- Joined
- Aug 21, 2021
- Messages
- 49
- Office Version
- 365
- Platform
- 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:
Current cribbed code - admittedly it may be a tad advanced for me at the moment.
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:
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