Find text and return the row number

ankita.sethi

Board Regular
Joined
Apr 27, 2011
Messages
58
Hi,

I am trying to write a VBA code where it takes a text value (given by the user( eg "reportname" , and searches it in a particular range (eg. A6 to A17), and then returns the row number in which the text was found.

Please help!
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Try

Code:
Sub test()
Dim Found As Range
Set Found = Range("A6:A17").Find(what:=InputBox("Enter search term"), LookIn:=xlValues, lookat:=xlWhole)
If Found Is Nothing Then
    MsgBox "Not found", vbExclamation
Else
    MsgBox "Found in row " & Found.Row, vbInformation
End If
End Sub
 
Upvote 0
In a standard module paste in:

Code:
Sub Foo()
Res = InputBox("Enter text for search")
With Sheet1.Range("A6:A17")
    Set c = .Find(Res, lookat:=xlWhole)
    If c Is Nothing Then
    MsgBox "Item not found"
    Exit Sub
    Else
    MsgBox "Your text is in row number " & c.Row
    End If
End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,590
Messages
6,179,754
Members
452,940
Latest member
rootytrip

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