Delete Row Select Case

L

Little Penny

I am trying to get this code to delete every row if the value in
column B begins with "X1".

I'm getting a syntax error any help would greatly be appreciated

Error here - Select Case Left((c, 2), 2)



Complete code:



Sub DeleteRowCase()
Dim c As Long
Dim LastRow As Long

Application.ScreenUpdating = False
LastRow = Range("C65536").End(xlUp).Row
For c = LastRow To 2 Step -1

Select Case Left((c, 2), 2)

Case Is = "X1": .EntireRow.Delete

Next c

End Select

Application.ScreenUpdating = True


End Sub
 
D

Dave Peterson

Select Case Left(cells(c, 2).value, 2)

or maybe better:

Select Case ucase(Left(cells(c, 2).value, 2))

And later...

Case Is = "X1": rows(c).Delete
or
Case Is = "X1": cells(c,2).EntireRow.Delete
 
D

Don Guillett

Sub deleteif()
For i = Cells(Rows.count, "c").End(xlUp).Row To 2 Step -1
Select Case UCase(Left(Cells(i, "c"), 2))
Case Is = "X1": Rows(i).Delete
Case Else
End Select
Next i
End Sub
 
L

Little Penny

Thank you Dave.





Select Case Left(cells(c, 2).value, 2)

or maybe better:

Select Case ucase(Left(cells(c, 2).value, 2))

And later...

Case Is = "X1": rows(c).Delete
or
Case Is = "X1": cells(c,2).EntireRow.Delete
 
L

Little Penny

Question.

I'm not a vba guru but why does the code work with declaring i as a
variable, and what is c?

Thanks
 
L

Little Penny

I just copied and pasted the code.I just don't understand why the
code worked without dim i as long.


Sub deleteif()
For i = Cells(Rows.count, "c").End(xlUp).Row To 2 Step -1
Select Case UCase(Left(Cells(i, "c"), 2))
Case Is = "X1": Rows(i).Delete
Case Else
End Select
Next i
End Sub
 
D

Dave Peterson

It's always good practice to declare your variables--but you don't absolutely
need to.

If you don't declare the variable, then excel/vba will make it a Variant
--just like if you explicitly did: Dim i as Variant

(I thought you wanted column B???)
 
L

Little Penny

Thanks


I do want column B. But now I understand the code and changes that I
need to make for any column.

Thanks Again
 

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