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.
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.