Deleting Rows Based on Criteria

B

bgoode

I'm a newbie to VBA and am trying to construct a simple macro to delete all
rows containing different characters.

For instance, delete all

- blank rows
- rows beginning "pgm"
- rows beginning "load"
- rows containing "total"

and some others.

I attempted to do this by copying a tutorial macro as below

Sub DeleteCells2()

Dim rng As Range
Dim i As Integer, counter As Integer

'Set the range to evaluate to rng.
Set rng = Range("A1:A10")

'initialize i to 1
i = 1

'Loop for a count of 1 to the number of rows
'in the range that you want to evaluate.
For counter = 1 To rng.Rows.Count

'If cell i in the range contains an "x",
'delete the row.
'Else increment i
If rng.Cells(i) = "x" Then
rng.Cells(i).EntireRow.Delete
Else
i = i + 1
End If

Next

End Sub


and then substituting my values for the "x". This took care of the blank
cells fine but when I insert "pgm*" or "=pgm*" instead of " " then nothing is
deleted. I haven't added additional if statments for the other criteria
because I want to make sure I can run this simple part and understand it.

Any suggestions would be appreaciated.
 
B

bgoode

Please ignore...now that I found this group and have read the others I have
some additional reading to do.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top