need VBA Selecting cells Containing specific Characters

pisdukas

New Member
Joined
Jun 12, 2019
Messages
7
Hello, Forum,

I am unable to find a VBA which would select cells which contain specific characters in a range. Could some give a hand?


Thank you for taking the time.
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Updated for "*WHI-*" with dash added before astrisk\

Code:
Sub DeleteRowss()
'
' DeleteRowss Macro
'




'
    Sheets("Sheet1").Select




    ActiveSheet.Range("A1").AutoFilter Field:=1, Criteria1:="*WHI-*", Operator:=xlFilterValues
    
    Range("A1").Select
    ActiveCell.Offset(1, 0).Rows("1:1").EntireRow.Select
    
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Delete Shift:=xlUp
    
    Worksheets("sheet1").AutoFilterMode = False




End Sub

Worked in Test File


Excel 2010
A
1item_sku
2HOD-UNI-0001-WHI
3HOD-UNI-0001-BLA-S
4HOD-UNI-0001-BLA-M
5HOD-UNI-0001-BLA-L
6HOD-UNI-0001-BLA-XL
7HOD-UNI-0001-BLA-XXL
8HOD-UNI-0001-ASH-S
9HOD-UNI-0001-ASH-M
10HOD-UNI-0001-ASH-L
11HOD-UNI-0001-ASH-XL
12HOD-UNI-0001-ASH-XXL
13HOD-UNI-0002-WHI
14HOD-UNI-0002-BLA-S
15HOD-UNI-0002-BLA-M
16HOD-UNI-0002-BLA-L
Sheet1
 
Upvote 0
Sorry, my bad. all of these cells supposed to remain. I have fixed the colors:https://rokastestas.s3.eu-west-3.amazonaws.com/sample2.xlsm

Here is another macro for you to try...
Code:
Sub DeleteRowsWithWHI()
  On Error Resume Next
  Application.ScreenUpdating = False
  With Range("A2", Cells(Rows.Count, "A").End(xlUp))
    .Value = Evaluate("IF(ISNUMBER(SEARCH(""-WHI-""," & .Address & ")),""""," & .Address & ")")
    .SpecialCells(xlBlanks).EntireRow.Delete
  End With
  Application.ScreenUpdating = True
  On Error GoTo 0
End Sub
 
Last edited:
Upvote 0
Here is another macro for you to try...
Code:
    .Value = Evaluate("IF(ISNUMBER(SEARCH(""-WHI-""," & .Address & ")),""""," & .Address & ")")
    [COLOR=#333333].SpecialCells(xlBlanks).EntireRow.Delete[/COLOR]
     
  
End Sub

Rick this is very cool, and so fast.
 
Last edited:
Upvote 0
Rick this is very cool, and so fast. This seems much faster than my macro. If this was an extreme amount of data, will this one work faster than a normal filter and delete?
Just wondering cause I have some data I may test that with
I don't know how it compares speedwise to a normal filter as I have never tested it... it was just something that came to me several years ago and I have been posting it as an alternative ever since.
 
Upvote 0
I don't know how it compares speedwise to a normal filter as I have never tested it... it was just something that came to me several years ago and I have been posting it as an alternative ever since.

Nice, I may test it out with some of my work reports, cause i'm looking for any kind of speed boost I can get. Thanks
 
Upvote 0
Here is another macro for you to try...
Code:
Sub DeleteRowsWithWHI()
  On Error Resume Next
  Application.ScreenUpdating = False
  With Range("A2", Cells(Rows.Count, "A").End(xlUp))
    .Value = Evaluate("IF(ISNUMBER(SEARCH(""-WHI-""," & .Address & ")),""""," & .Address & ")")
    .SpecialCells(xlBlanks).EntireRow.Delete
  End With
  Application.ScreenUpdating = True
  On Error GoTo 0
End Sub


I am having trouble implementing your code into my VBA, so I made that it would run you macro within my macro. But it replaces blank cells with 0 and replaces "WHI" with "ASH".

https://i.ibb.co/zZgjDTV/vba.jpg

o1ea5j

Code:
Sub HOD_01()
'
' HOD_01 Macro
'


'
Set wkb = Workbooks.Open(Filename:="D:\Parent.csv")
  
     Columns("B:B").Select
    Selection.Replace What:="TEE-VYR", Replacement:="HOD-UNI", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
        Selection.Replace What:="WHI", Replacement:="ASH", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
        Range("B4:B1048576").Select
        Range("B1048576").Activate
        Selection.Copy
        Selection.Replace What:="TEE", Replacement:="HOD", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
        
        Application.Run "'sample2.xlsm'!Delete"
        
        ThisFile = "HOD_01"
       Application.ScreenUpdating = True
ActiveWorkbook.SaveAs Filename:="D:\" & ThisFile & ".txt", FileFormat:=xlText
Workbooks("HOD_01.txt").Close SaveChanges:=False
End Sub


Sub Delete()
  On Error Resume Next
  Application.ScreenUpdating = False
  With Range("B2", Cells(Rows.Count, "B").End(xlUp))
    .Value = Evaluate("IF(ISNUMBER(SEARCH(""-WHI-""," & .Address & ")),""""," & .Address & ")")
    .SpecialCells(xlBlanks).EntireRow.Delete
  End With
  Application.ScreenUpdating = True
 
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,178
Members
453,021
Latest member
Justyna P

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