VBA Delete Area if first to characters start with "LS" or "BL"

Stephen_IV

Well-known Member
Joined
Mar 17, 2003
Messages
1,168
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
I have course numbers in Column E. If the first to characters start with "LS" or "BL" then I would like the entire student deleted. I highlighted it in red so you can see the ones I would like deleted.


Book10
ABCDEF
1ST #LASTFIRSTTERMIDSCHOOLIDCOURSE_NUMBERCOURSE_NAME
2
31111xxxx xxxxxxxx2702111111MA301YMAMAPROBABILITY & STATISTICS - MAGNET
41111xxxx xxxxxxxx2700111111AD011YPLGEHOMEROOM GRADE 11
51111xxxx xxxxxxxx2702111111LS102YTDAPAP CAPSTONE 1-AP SEMINAR
61111xxxx xxxxxxxx2702111111PE100YTDGEFULL YEAR PHYSICAL EDUCATION
71111xxxx xxxxxxxx2702111111SS301YMAMAUS HISTORY - MAGNET
8
92222xxxx xxxxxxxx2702222222SC110YMAMABIOLOGY - MAGNET
102222xxxx xxxxxxxx2702222222EN101YMAMAENGLISH 9 - MAGNET
112222xxxx xxxxxxxx2702222222MA101YMAMAALGEBRA I - MAGNET
122222xxxx xxxxxxxx2702222222CS111YMAMAINTRODUCTION TO JAVA
132222xxxx xxxxxxxx2700222222AD009YPLGEHOMEROOM GRADE 9
14
153333xxxx xxxxxxxx2700111111AD009YPLGEHOMEROOM GRADE 9
163333xxxx xxxxxxxx2702111111BL101YMAMAENGLISH 9 - MAGNET
173333xxxx xxxxxxxx2702111111MA101YMAMAALGEBRA I - MAGNET
183333xxxx xxxxxxxx2702111111CS111YMAMAINTRODUCTION TO JAVA
193333xxxx xxxxxxxx2702111111WL101YMAMASPANISH I - MAGNET
20
214444xxxx xxxxxxxx2702333333SC311YMAMAHUMAN PHYSIOLOGY
224444xxxx xxxxxxxx2702333333MA202YMAMAALGEBRA II - MAGNET
234444xxxx xxxxxxxx2700333333AD010YPLGEHOMEROOM GRADE 10
244444xxxx xxxxxxxx2706333333SS412SMAMAEARLY AMERICAN REPUBLIC - MAGNET
254444xxxx xxxxxxxx2702333333SC420YMAMAINTRODUCTION TO PHARMACOLOGY
264444xxxx xxxxxxxx2705333333SS201SMAMACIVICS - MAGNET
27
285555xxxx xxxxxxxx2700222222AD009YPLGEHOMEROOM GRADE 9
295555xxxx xxxxxxxx2702222222WL101YMAMASPANISH I - MAGNET
305555xxxx xxxxxxxx2702222222MA101YMAMAALGEBRA I - MAGNET
315555xxxx xxxxxxxx2702222222SC110YMAMABIOLOGY - MAGNET
325555xxxx xxxxxxxx2705222222SS201SMAMACIVICS - MAGNET
335555xxxx xxxxxxxx2706222222SS412SMAMAEARLY AMERICAN REPUBLIC - MAGNET
34
357777xxxx xxxxxxxx2702333333SC110YMAMABIOLOGY - MAGNET
367777xxxx xxxxxxxx2700333333LS012YPLGEHOMEROOM GRADE 12
377777xxxx xxxxxxxx2702333333WL101YMAMASPANISH I - MAGNET
387777xxxx xxxxxxxx2706333333SS412SMAMAEARLY AMERICAN REPUBLIC - MAGNET
397777xxxx xxxxxxxx2705333333SS201SMAMACIVICS - MAGNET
407777xxxx xxxxxxxx2700333333AD009YPLGEHOMEROOM GRADE 9
41
428888xxxx xxxxxxxx2702111111AD014YPLGEEARLY DISMISSAL
438888xxxx xxxxxxxx2700111111AD012YPLGEHOMEROOM GRADE 12
448888xxxx xxxxxxxx2702111111CS137YMAMADIGITAL ACTIVISM/SOCIETY
458888xxxx xxxxxxxx2706111111HE101STDGEHEALTH
468888xxxx xxxxxxxx2705111111MA507SMAMABUSINESS MATH
478888xxxx xxxxxxxx2702111111EN401YMAMAENGLISH 12 - MAGNET
48
Sheet1
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Hi.
VBA Code:
Sub test()
    Dim lst, i&, ky$, y, sil As Boolean, rng As Range, lr&
    lr = Cells(Rows.Count, 1).End(3).Row
    lst = Range("A1:F" & lr).Value

    Set rng = Range(lr + 2 & ":" & lr + 2)
    With CreateObject("Scripting.Dictionary")
        For i = 3 To UBound(lst)
            ky = lst(i, 1)
            sil = (Left(lst(i, 5), 2) = "LS" Or Left(lst(i, 5), 2) = "BL")
            If Not .exists(ky) Then
                .Item(ky) = Array(i, i, sil)
            Else
                y = .Item(ky)
                y(1) = i
                If y(2) <> True Then y(2) = sil
                .Item(ky) = y
            End If
        Next i
        For Each y In .items
            If y(2) = True Then
                Set rng = Union(rng, Range(y(0) & ":" & y(1) + 1))
            End If
        Next y
        rng.Delete
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,237
Messages
6,123,800
Members
449,127
Latest member
Cyko

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