Macro to find a cell with a specific word (when its mixed in with a sentence)

sdoppke

Well-known Member
Joined
Jun 10, 2010
Messages
647
Hi everyone, im try to build a macro that will find a word within a sentence that is in a cell. i.e.

A1="store number 566" I need the macro to select that cell (and stop). After that I will insert the rest of the code to copy and paste a larger area from that point, into another worksheet.

Thanks everyone in advance for any help. :)

sd
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Try something like this...
Code:
Sub Find_Word()
    
    Dim FindWord As String, Found As Range

    FindWord = [COLOR="Red"]"store number 566"[/COLOR]

    Set Found = Sheets("Sheet1").Cells.Find(What:=FindWord, _
                                            LookIn:=xlValues, _
                                            LookAt:=xlPart, _
                                            SearchOrder:=xlByRows, _
                                            SearchDirection:=xlNext, _
                                            MatchCase:=False)
                                        
    If Not Found Is Nothing Then
        Found.Select
    Else
        MsgBox "No match found."
    End If
        
End Sub
 
Upvote 0
AlphaFrog thanks for teh fast reply! this is great, would you be able to help me modify your script to look through all open workbooks? also the im thinking to make it search faster (because there are 1000's of rows) could we set it up to search only column A in the open workbooks (thats were the data will be).

thanks a ton for your help. if youi happend to describe what you came up with, it wouild be really helpfull so i can learn and help others. :)

sd
 
Upvote 0
Try something like this...
Code:
Sub Find_Word()
 
    Dim FindWord As String, Found As Range
 
    FindWord = [COLOR=red]"store number 566"[/COLOR]
 
    Set Found = Sheets("Sheet1").Cells.Find(What:=FindWord, _
                                            LookIn:=xlValues, _
                                            LookAt:=xlPart, _
                                            SearchOrder:=xlByRows, _
                                            SearchDirection:=xlNext, _
                                            MatchCase:=False)
 
    If Not Found Is Nothing Then
        Found.Select
    Else
        MsgBox "No match found."
    End If
 
End Sub


I thought i wuold post what i have so far:

Code:
Sub Business_Results()
    Dim FindWord As String, Found As Range
    FindWord = Sheets("MyStoreInfo").Range("B2")
    Set Found = Sheets("Sheet2").Cells.Find(What:=FindWord, _
                                            LookIn:=xlValues, _
                                            LookAt:=xlPart, _
                                            SearchOrder:=xlByRows, _
                                            SearchDirection:=xlNext, _
                                            MatchCase:=False)
                                        
    If Not Found Is Nothing Then
        Found.Select
        Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Copy
    Sheets("Sheet3").Select
    Range("B4").Select
    ActiveSheet.Paste
    Range("B4").Select
    Else
        MsgBox "Cannot find store data..."
    End If

End Sub

Thanks again AlphaFrog

sd
 
Upvote 0
...modify your script to look through all open workbooks?

So you want to copy the found store data from each open workbook to Sheet3. In that correct?

Do you want to search each worksheet in each open workbook or just Sheet2 in each workbook?
 
Upvote 0
So you want to copy the found store data from each open workbook to Sheet3. In that correct?

Do you want to search each worksheet in each open workbook or just Sheet2 in each workbook?

that's exactly right. I need to be able to search all worksheets in all workbooks, column A only.

Sd
 
Upvote 0
Do you want to search each worksheet in each open workbook or just Sheet2 in each workbook?

Ive tried to modify your script myself and seemed to have broken it all together LOL.

It would need to be able to seach all opend sheet on all open workbooks. The reason the search needs to be "all open" is becasue they change the file name every day (by date).

Hope that helps, and thanks for the help.

sd
 
Upvote 0
Once a match is found on a given sheet, what exactly do you want copied (what cell range relative to the found match) and where exactly do you want it copied?

It looks like (but I'm not sure) you want to copy all rows starting from the match on down and pate them to the next empty row on Sheet3. Please clarify with details.
 
Upvote 0
It looks like (but I'm not sure) you want to copy all rows starting from the match on down and pate them to the next empty row on Sheet3. Please clarify with details.

I was looking to use the:

Code:
Range(Selection, Selection.End(xlToRight)).Select
        Range(Selection, Selection.End(xlDown)).Select
        Selection.Copy
        Sheets("GRL Copy and Paste").Select
        Range("A1").Select
        ActiveSheet.Paste

to copy everything to the right and then everything from there, down (a large group of data that veries in size each time), then paste it to the "GRL Copy and Paste" WS.

thanks

sd
 
Upvote 0
Code:
Sub Business_Results()

    Dim FindWord As String, Found As Range
    Dim wsDest As Worksheet, ws As Worksheet, wb As Workbook
    Dim Nextrow As Long, Lastrow As Long

    Set wsDest = ThisWorkbook.Sheets("Sheet3")
    FindWord = ThisWorkbook.Sheets("MyStoreInfo").Range("B2").Value
    
    Application.ScreenUpdating = False
    For Each wb In Application.Workbooks        ' Loop through each open workbook
        If wb.Name <> ThisWorkbook.Name Then    ' Exclude this workbook
            For Each ws In wb.Sheets            ' Loop through each worksheet of each workbook
                Set Found = ws.Range("A:A").Find(What:=FindWord, _
                                                 LookIn:=xlValues, _
                                                 LookAt:=xlPart, _
                                                 SearchOrder:=xlByRows, _
                                                 SearchDirection:=xlNext, _
                                                 MatchCase:=False)
                If Not Found Is Nothing Then
                    Nextrow = wsDest.Cells.Find("*", , , , xlByRows, xlPrevious).Row + 1 ' Next empty row on Sheet3
                    Lastrow = ws.Cells.Find("*", , , , xlByRows, xlPrevious).Row         ' Last used row on Store sheet
                    ' Copy\Paste found store data to the next empty row on Sheet3
                    ws.Range(Found, Found.End(xlToRight)).Resize(Lastrow - Found.Row + 1).Copy _
                        Destination:=wsDest.Range("B" & Nextrow)
                End If
            Next ws
        End If
    Next wb
    Application.ScreenUpdating = True
    MsgBox "Copy complete.", vbInformation, "Copy Store Data"

End Sub
 
Upvote 0

Forum statistics

Threads
1,224,609
Messages
6,179,879
Members
452,948
Latest member
Dupuhini

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