Saving value of Search String

phiero21

Board Regular
Joined
Sep 20, 2007
Messages
136
Is it possible to save the text of the search string in a particular cell using VBA/Macro.

So, if a user searches for "Bed", the text within A1 should change to "Bed"
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Hi phiero21,

Try this while on the sheet with the text to be found:

VBA Code:
Option Explicit
Sub Macro1()

    Dim strText As String
    Dim rngFoundCell As Range

    strText = InputBox("Please enter the text to be searched:", "Text Search")
   
    If Len(strText) = 0 Then Exit Sub 'Either nothing was entered or <Cancel> was pressed
   
    Set rngFoundCell = Cells.Find(What:=strText, After:=ActiveCell, LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)
   
    Range("A1").Value = strText
   
    If rngFoundCell Is Nothing Then
        MsgBox "There were no cells in the sheet containing the text """ & strText & """.", vbExclamation, "Text Search"
    End If
   
End Sub

Regards,

Robert
 
Upvote 0

Forum statistics

Threads
1,215,073
Messages
6,122,977
Members
449,095
Latest member
Mr Hughes

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