Macro for cells containing certain text

benjg07

New Member
Joined
Nov 24, 2017
Messages
2
Hi there :nya:

Need some help. Currently this macro only finds cells containing the exact text entered into the text box Search_OrganName.

I would like it to find any cell from the range that contains that text rather than just exact matches.

I'm thinking there is a really obvious solution I'm just not seeing.

Hope this question makes sense.

Thanks people

Rich (BB code):
Rich (BB code):
Sub searchontacts_specific()


'Marco to search for a specific contact by Organisation Name from the database


Dim Search_OrganName As String
Dim i As Integer


Sheet3.Range("SearchResults").ClearContents


Search_OrganName = Sheet3.Search_OrganName.Value


Sheet4.Activate


finalrow = Sheet4.Range("B100000").End(xlUp).Row


For i = 2 To finalrow


    If Cells(i, 2) = Search_OrganName Then


'Here is the problem. It only finds exact matches to the text entered into Search_OrganName text box
'I would like it to find all cells containing text entered
    
        Range(Cells(i, 1), Cells(i, 14)).Copy


        Sheet3.Activate


        Range("L300").End(xlUp).Offset(1, 0).PasteSpecial


        End If


Sheet4.Activate


Next i


Sheet3.Activate


MsgBox "Search Completed. Good Job!"


End Sub
 
Last edited by a moderator:

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Change this
Code:
If Cells(i, 2) = Search_OrganName Then
To this
Code:
If InStr(Cells(i, 2), " " & Search_OrganName & " ") > 0 Then
 
Upvote 0
Hi thanks

Really helped. Ended up learning a lot about the InStr function.

Code:
 If InStr(Cells(i, 2), Search_OrganName) > 0 Or Cells(i, 2) = Search_OrganName Then

ended up using this and it works really well.

Thanks
 
Upvote 0
Hi thanks

Really helped. Ended up learning a lot about the InStr function.

Code:
 If InStr(Cells(i, 2), Search_OrganName) > 0 Or Cells(i, 2) = Search_OrganName Then


ended up using this and it works really well.

Thanks

I would think the Or part is unnecessary, but if it works for you then that is what counts.
Regards, JLG
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,876
Members
452,363
Latest member
merico17

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