Case statement

wildturkey

Board Regular
Joined
Feb 21, 2006
Messages
189
Office Version
  1. 365
Platform
  1. Windows
I have the following code which should look down a sheet and delete all rows where either Course or Consultancy appear in column B, however it doesnt:) Any ideas please........

Range("A65000").Select
Selection.End(xlUp).Select

xlastrow = ActiveCell.Row

Range("b1").Select

For i = 1 To xlastrow

xtype = ActiveCell.Value

Select Case xtype

Case "Course"
ActiveCell.Rows("1:1").EntireRow.Select
Selection.EntireRow.Delete

Case "Consultancy"
ActiveCell.Rows("1:1").EntireRow.Select
Selection.EntireRow.Delete

End Select

ActiveCell.Offset(1, 0).Select


Next i
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Hi

If you want to do it like this then you need to loop thru the range backwards ie from xLastRow to 1 Step -1 (as otherwise the numbering gets stuffed up when you have deleted a row).

There are easier ways however, such as using autofilter.
 
Upvote 0
Many thanks, went round the trees a few times more and have now got it.
 
Upvote 0
Id had problems with auto filter in the past having large worksheets and not knowing the last row, or how many rows I would need to delete. This way just seams to be a little neater.

However unless I can find away in vba to identify a cell that contains the letter x then auto filter will have to be used.

Cheers Both
 
Upvote 0
However unless I can find away in vba to identify a cell that contains the letter x then auto filter will have to be used.

Cheers Both

Code:
Sub test()
If InStr(1, Range("A1").Value, "x", vbTextCompare) > 0 Then
    'do something
End If
End Sub
 
Upvote 0
Thanks both,

Ive just got the autofilter working nicely including the delete and that formula for containing x will come in handy!!

Cheers
 
Upvote 0

Forum statistics

Threads
1,215,329
Messages
6,124,302
Members
449,150
Latest member
NyDarR

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top