Vba help

Donai

Well-known Member
Joined
Mar 28, 2009
Messages
543
Hi, the below code deletes everything that does not start with "W", how can i add another case where i need data that starts with "SMP", so delete everything that does not start with "W" and "SMP"


Code:
Public Sub ClearColB2()
Dim i   As Long, _
    LR  As Long

Dim ShtRec As Excel.Worksheet

Set ShtRec = Sheets(gstrRec)

LR = ShtRec.Range("B" & Rows.Count).End(xlUp).Row
Application.ScreenUpdating = False
For i = LR To 6 Step -1
    Select Case Left(ShtRec.Range("B" & i).Value, 1)
        Case "W"
        Case Else
            ShtRec.Rows(i).Delete
    End Select
    
Next i

End Sub
 
Jon,
I think the OP wanted to delete rows that don't begin with "W" or "SMP". Your code deletes these rows unless I'm missing something?
 
Upvote 0

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Hi Yard, i need the below code to delete everything that starts with W, Z, SMP and a number


Try
Code:
Public Sub ClearColB2()
Dim i As Long, LR As Long, s As String
Dim ShtRec As Excel.Worksheet

Set ShtRec = Sheets(gstrRec)

LR = ShtRec.Range("B" & Rows.Count).End(xlUp).Row
Application.ScreenUpdating = False
For i = LR To 6 Step -1
    s = CStr(ShtRec.Range("B" & i).Value)
    If s Like "W*" Then
        'do nothing
    ElseIf s Like "SMP*" Then
        'do nothing
    Else
        ShtRec.Rows(i).Delete
    End If
Next i

Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,587
Messages
6,179,738
Members
452,940
Latest member
Lawrenceiow

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