Find cells that contain some of text string

Greemo

New Member
Joined
May 22, 2014
Messages
42
Hello,

Can I have some direction on creating an excel macro to select all the cells that start with a specific string of text? Should I use the find tool? This string of text will always be the start of the cells I'm looking for but will never be the entire contents of the cell.
Any help is appreciated. Thanks.

Greemo
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Method 1

(1) Use the Find command (Home --> Editing --> Find & Select --> Find...).
(2) Type the text you want to find in the Find what textbox followed by an asterisk (*).
(3) Check the Match entire cell contents checkbox and click the Find All button.
(4) Select all the found cell(s) by clicking on the first one in the list and then, while holding down the Shift key, clicking on the last one if the list.


Method 2

Use the following macro:

Code:
Sub FindText()

    Dim SearchRange As Range
    Dim FoundRange As Range
    Dim Cell As Range
    Dim Txt As String
    
    If TypeName(ActiveSheet) <> "Worksheet" Then Exit Sub
    
    'Set range to search
    Set SearchRange = ActiveSheet.UsedRange
    
    'Set text to search for
    Txt = "hello"
    
    For Each Cell In SearchRange
        If Cell.Value Like Txt & "*" Then
            If FoundRange Is Nothing Then
                Set FoundRange = Cell
            Else
                Set FoundRange = Union(FoundRange, Cell)
            End If
        End If
    Next Cell
    
    If Not FoundRange Is Nothing Then FoundRange.Select

End Sub
 
Upvote 0
Hi gpeacock,

Thank you. The macro works perfectly! Any way to have it only search at non blank cells to save resources?

Thank you,

Greemo
 
Upvote 0
Try:


Code:
Set SearchRange = ActiveSheet.UsedRange.SpecialCells(xlCellTypeConstants)
</pre>
 
Upvote 0

Forum statistics

Threads
1,214,970
Messages
6,122,514
Members
449,088
Latest member
RandomExceller01

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