VBA: Is there a way to write a macro to delete rows that begin with certain characters?


Posted by Joe on October 06, 2000 12:52 PM

I'm trying to write a VBA macro that filters columns which begin with similar characters(e.g. Date:)but differs later in the row, and then deletes them from the spreadsheet. There could be hundreds of rows that need to be deleted. Is there a way?



Posted by Celia on October 06, 2000 6:04 PM


Joe
The following will delete the entire row where the cell in column A starts with "Date:" :-

Sub Delete()
Dim theCol As Range, cell As Range, RtoDel As Range
Dim LtoDel As String

Set theCol = Range(Range("A1"), Range("A65536").End(xlUp))
LtoDel = "Date:"

For Each cell In theCol
If Left(cell, 5) = LtoDel Then
If RtoDel Is Nothing Then
Set RtoDel = cell
Else
Set RtoDel = Application.Union(RtoDel, cell)
End If
End If
Next
On Error GoTo e
RtoDel.EntireRow.Delete
Exit Sub
e:
MsgBox "There is nothing to delete"
End Sub