Remove Cells That Don't Begin with One of A List of Inputs

joshmorris23x

New Member
Joined
May 8, 2017
Messages
20
Hi all,

I'm looking for a macro, that when executed, would remove all cells that don't begin with one of 3 different

For example, if I had these cells in column A:

AA
AB
BC
CD
EA
FA

And I wanted to remove anything that didn't start with "A" OR "B" OR "C"
I would be left with just these cells:

EA
FA

Any and all help is appreciated :)
Thanks,
Josh
 
Re: How to Remove Cells That Don't Begin with One of A List of Inputs

If you wanted to end with just EA and FA you would need to remove rows that did start with A or B or C.

joshmorris23x,

If the above quote is correct, then try the following macro that does not do any looping in the rows in column A.


Code:
Sub joshmorris23x90()
'hiker95, 10/01/2017, ME1025108
Application.ScreenUpdating = False
Dim Addr As String
Addr = "A1:A" & Cells(Rows.Count, "A").End(xlUp).Row
Range(Addr) = Evaluate(Replace("IF(LEFT(@,1)=""A"",""#N/A"",@)", "@", Addr))
Range(Addr) = Evaluate(Replace("IF(LEFT(@,1)=""B"",""#N/A"",@)", "@", Addr))
Range(Addr) = Evaluate(Replace("IF(LEFT(@,1)=""C"",""#N/A"",@)", "@", Addr))
On Error GoTo NoDeletes
Columns("A").SpecialCells(xlConstants, xlErrors).EntireRow.Delete
NoDeletes:
Application.ScreenUpdating = True
End Sub
 
Upvote 0

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Re: How to Remove Cells That Don't Begin with One of A List of Inputs

There are more. I'd like to be able to change the # of strings, but there would be no more than 5 of them.
Also, they vary in length, and won't necessarily be the same # of characters.
OK, thanks. Give this a test on a copy of your workbook.
Rich (BB code):
Sub Delete_Rows_Josh()
  Dim a As Variant, b As Variant
  Dim i As Long, k As Long
 
  With Range("A1", Range("A" & Rows.Count).End(xlUp)).Resize(, 2)
    a = .Columns(1).Value
    ReDim b(1 To UBound(a), 1 To 1)
    With CreateObject("VBScript.RegExp")
      .Pattern = "GHI|MNO|UVWXYZ|KK" '<- add more if you want, using this pattern
      For i = 1 To UBound(a)
        If Not .Test(a(i, 1)) Then
          b(i, 1) = 1
          k = k + 1
        End If
      Next i
    End With
    If k > 0 Then
      Application.ScreenUpdating = False
      .Columns(2).Value = b
      .Sort Key1:=.Columns(2), Order1:=xlAscending, Header:=xlNo, OrderCustom:=1, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
      .Resize(k).EntireRow.Delete
      Application.ScreenUpdating = True
      End If
  End With
End Sub
 
Last edited:
Upvote 0
Re: How to Remove Cells That Don't Begin with One of A List of Inputs

Here is another macro you can try...
Code:
[table="width: 500"]
[tr]
	[td]Sub DeleteRowsJosh()
  Dim X As Long, NextRow As Long, Arr As Variant, Keep As Variant, Temp As Variant
  Keep = Split("[B][COLOR="#FF0000"]GHI|MNO|UVWXYZ|KK[/COLOR][/B]", "|")  [B][COLOR="#008000"]'<- add more if you want, using this pattern[/COLOR][/B]
  Arr = Application.Transpose(Range("A1", Cells(Rows.Count, "A").End(xlUp)))
  For X = 0 To UBound(Keep)
    Temp = Filter(Arr, Keep(X))
    NextRow = Cells(Rows.Count, "B").End(xlUp).Offset(1).Row
    If NextRow = 2 Then If Range("B1") = "" Then NextRow = 1
    If UBound(Temp) > -1 Then Cells(NextRow, "B").Resize(UBound(Temp) + 1) = Application.Transpose(Temp)
  Next
  Columns("A").Delete
End Sub[/td]
[/tr]
[/table]

Note: While the output from this macro is the same as from Peter's macro, the order of the items in the outputted list differs. Peter's code keeps the outputted items in the same order that they appear in the original list whereas my code outputs them in the order of the text searched for; that is, for the "Keep" list shown in the code above, all the text containing "GHI" will be listed first, after that will be listed all of the text containing "MNO", and so on.
 
Last edited:
Upvote 0
Re: How to Remove Cells That Don't Begin with One of A List of Inputs

Peter, that macro works flawlessly, thank you!


Rick, that is an interesting variation. If I require that variation, i'll make sure to use it!

Thanks everyone!
 
Upvote 0
BTW, Rick's idea of using an array filter could be used and still keep the items in order. Here is an adaptation to do that and it is also very fast. In fact, with my test data and about 5 strings to check for, a little faster than my earlier code, but with both only taking about one tenth of a second for 25,000 rows who cares about the tiny difference? :)

Anyway, if you wanted to give it a whirl..
Code:
Sub DeleteRowsJM()
  Dim s As String
  Dim Keep As Variant, itm As Variant
 
  With Range("A1", Cells(Rows.Count, "A").End(xlUp))
    s = Join(Application.Transpose(.Value), "|")
    .ClearContents
    Keep = Split("GHI|MNO|UVWXYZ|KK", "|")  '<- add more if you want, using this pattern
    For Each itm In Keep
      s = Replace(s, itm, "^" & itm)
    Next itm
    Keep = Split(Replace(Join(Filter(Split(s, "|"), "^"), "|"), "^", ""), "|")
    If UBound(Keep) > -1 Then .Resize(UBound(Keep) + 1).Value = Application.Transpose(Keep)
  End With
End Sub
I've assumed "|" and "^" are characters that would not appear in the raw data.
 
Upvote 0
Re: How to Remove Cells That Don't Begin with One of A List of Inputs

OK, thanks. Give this a test on a copy of your workbook.
Rich (BB code):
Sub Delete_Rows_Josh()
  Dim a As Variant, b As Variant
  Dim i As Long, k As Long
 
  With Range("A1", Range("A" & Rows.Count).End(xlUp)).Resize(, 2)
    a = .Columns(1).Value
    ReDim b(1 To UBound(a), 1 To 1)
    With CreateObject("VBScript.RegExp")
      .Pattern = "GHI|MNO|UVWXYZ|KK" '<- add more if you want, using this pattern
      For i = 1 To UBound(a)
        If Not .Test(a(i, 1)) Then
          b(i, 1) = 1
          k = k + 1
        End If
      Next i
    End With
    If k > 0 Then
      Application.ScreenUpdating = False
      .Columns(2).Value = b
      .Sort Key1:=.Columns(2), Order1:=xlAscending, Header:=xlNo, OrderCustom:=1, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
      .Resize(k).EntireRow.Delete
      Application.ScreenUpdating = True
      End If
  End With
End Sub


Hey, I know this is an older thread, but I have a quick request for an update on this macro:

How can I change this macro so that instead of just running on Sheet1 Column A, it loops and runs on Sheets1-10 (Sheet1, Sheet2, Sheet3, Sheet4, Sheet5, Sheet6, Sheet7, Sheet8, Sheet9, Sheet10)'s A columns?

Thanks!
 
Upvote 0
Re: How to Remove Cells That Don't Begin with One of A List of Inputs

How can I change this macro so that instead of just running on Sheet1 Column A, it loops and runs on Sheets1-10 (Sheet1, Sheet2, Sheet3, Sheet4, Sheet5, Sheet6, Sheet7, Sheet8, Sheet9, Sheet10)'s A columns?
Try
Code:
Sub Delete_Rows_Josh_MultiSheet()
  Dim a As Variant, b As Variant
  Dim i As Long, k As Long, sh As Long
  
  For sh = 1 To 10
    k = 0
    With Sheets("Sheet" & sh)
      With .Range("A1", .Range("A" & .Rows.Count).End(xlUp)).Resize(, 2)
        a = .Columns(1).Value
        ReDim b(1 To UBound(a), 1 To 1)
        With CreateObject("VBScript.RegExp")
          .Pattern = "GHI|MNO|UVWXYZ|KK" '<- add more if you want, using this pattern
          For i = 1 To UBound(a)
            If Not .Test(a(i, 1)) Then
              b(i, 1) = 1
              k = k + 1
            End If
          Next i
        End With
        If k > 0 Then
          Application.ScreenUpdating = False
          .Columns(2).Value = b
          .Sort Key1:=.Columns(2), Order1:=xlAscending, Header:=xlNo, OrderCustom:=1, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
          .Resize(k).EntireRow.Delete
          Application.ScreenUpdating = True
          End If
      End With
    End With
  Next sh
End Sub
 
Upvote 0
Re: How to Remove Cells That Don't Begin with One of A List of Inputs

Hello,

How can, one of the VBA codes, be changed to delete only the cell in column A, not the whole row.

Thank you.
 
Upvote 0

Forum statistics

Threads
1,215,641
Messages
6,125,982
Members
449,276
Latest member
surendra75

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