VBA code to return row number of found value

Majawat

New Member
Joined
Jun 14, 2011
Messages
49
I have two sheets: Source and Database

Database has two columns, A is a list of names, B is a list of numbers

I would like a VBA code in Source to search for a user-inputted name inside Database column A and then return which row it's located.

Suggestions?

Thanks!
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Try

Code:
Sub Asearch()
Dim found As Range
Set found = Sheets("Database").Columns("A").Find(what:=InputBox("Enter search term"), LookIn:=xlValues, lookat:=xlValues)
If found Is Nothing Then
    MsgBox "Not found"
Else
    MsgBox "Found on row " & found.Row
End If
End Sub
 
Upvote 0
I get a "Subscript out of range" error at

Code:
Set found = Sheets("Ratings").Columns("A").Find(what:=ActiveSheet.Cells(3, 1), LookIn:=xlValues, lookat:=xlValues)

"Ratings" is the true name of the Database. And I'm using the cell A3 as the input.

I notice LookIn is caps, but lookat isn't... Is there a reason for that?
 
Upvote 0
Sorry, it should be

Rich (BB code):
Set found = Sheets("Ratings").Columns("A").Find(what:=ActiveSheet.Cells(3, 1).Value, LookIn:=xlValues, lookat:=xlWhole)
 
Upvote 0
Sorry, it should be

Rich (BB code):
Set found = Sheets("Ratings").Columns("A").Find(what:=ActiveSheet.Cells(3, 1).Value, LookIn:=xlValues, lookat:=xlWhole)

HI,
Can you please tell me how to return row numbers if the name exists more than once in the above code?

THank you,
Bharat
 
Upvote 0
Do you know why this code wouldn't be working?
Code:
Private Sub CommandButton21_Click()Dim c As String
For Each c.Str In Worksheets("Sheet7").Range("C2")
   For Each c.Str In Worksheets("Sheet2").Range("E2:E895")
       n = found.Row
       project = Worksheets("Sheet2").Range("Ln")
       Weight = Worksheets("Sheet2").Range("Fn")
       MsgBox project, Weight
       
    Next c


Next c


End Sub
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,285
Members
452,902
Latest member
Knuddeluff

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