Delete Row syntax conundrum

U

username

I have a button where Users can click to create new rows to specif
'Audiences' for a communication. When they click the button a ne
Audience row with a cell with a drop down in appears each time. Howeve
when I want to 'Reset' the worksheet I only want there to be on
Audience row remaining.

I guess I need to check the cell to see if there is a list item in i
and if so delete the row that the cell is in.

I have:

Sub IfStatement()
If Range("A20") = List Then Rows("20:20").Select & Selection.Delet
& Shift:=xlUp
End Sub

But I get compile errors - probably because I have the wrong syntax
I'm not massively technical so can anyone help out on my syntax at al
please?

Thank you very much
 
B

Bernie Deitrick

If your Audience rows can be anywhere below row 20, then you could try
something like this:

Sub IfStatement2()
Dim myList As Variant
Dim i As Long
myList = Array("Fred", "Sue", "Tim", "Tom")
For i = Range("A65536").End(xlUp).Row To 21 Step -1
If Not IsError(Application.Match(Range("A" & i).Value, myList)) Then
Range("A" & i).EntireRow.Delete
End If
Next i
End Sub

Note that the line
myList = Array("Fred", "Sue", "Tim", "Tom")
should be modified to include those values that indicate that the row is an
"Audience" row.

HTH,
Bernie
MS Excel MVP
 
U

username

This seems to work well - for which, thanks.

The only issue is that if I specify in myList array (for example) "a"
"b", "c" & "d" as the characters to search for and I put in anothe
character into one of cells after A21, for example "s", when I press m
'Delete Row' button it also deletes the "s" as well as any of th
specified characters. Any ideas how to make it only delete thos
characters/words that I have specified in myList?

Thanks again
 
U

username

It seems that your code works a little too well!

I have specified the items in the myList array accurately, but th
'match' test seems to be a little loose in its application. Any text i
the column beneath my Audiences (even with no relation to the tex
specified in the myList array) also seems to bring up a match and thu
its row gets deleted.

Is there a way to force an 'exact match'?

Many thanks agai
 
B

Bernie Deitrick

My bad. The line

If Not IsError(Application.Match(Range("A" & i).Value, myList)) Then

should be

If Not IsError(Application.Match(Range("A" & i).Value, myList, False)) Then

Sorry about that.

HTH,
Bernie
MS Excel MVP
 
B

Bernie Deitrick

The line

If Not IsError(Application.Match(Range("A" & i).Value, myList)) Then

should be

If Not IsError(Application.Match(Range("A" & i).Value, myList, False)) Then

Sorry about that.

HTH,
Bernie
MS Excel MVP
 
Top