Use Contents of Cell in a Find

vlswanson

New Member
Joined
Jul 7, 2006
Messages
16
Office Version
  1. 2021
Platform
  1. Windows
Hi,

I'm hung up on something that seems so simple! As part of a larger process, I want to find the row that contains a value in column A that corresponds with the information in another cell. I did some searching and found this code:

Public Function GetRowNum(SheetName As String, SearchVal As String) As Long
GetRowNum = Sheets(SheetName).Columns(1).Cells.Find(SearchVal).Row
End Function

Sub FindRow()
Dim MyRow As Long
MyRow = GetRowNum("Db", "3650")
Range("A" & MyRow).Select
End Sub

This works great if one manually enters the value being searched for (the "3650" above), however I want to use the contents of cell B2 where the "3650" is. I've played around with this but haven't found the right combination :( I'd appreciate your help.
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
If cell B2 contains "3650", try removing the quote marks - just enter the number 3650 in B2 then change this line:

MyRow = GetRowNum("Db", "3650")

to this:

MyRow = GetRowNum("Db", [B2])
 
Upvote 0
How about
Code:
Sub test()
   Dim Fnd As Range
   Dim MyRow As Long
   Set Fnd = Range("A:A").Find(Range("B2").Value, , , xlWhole, , , False, , False)
   If Not Fnd Is Nothing Then MyRow = Fnd.Row
End Sub
 
Upvote 0
If cell B2 contains "3650", try removing the quote marks - just enter the number 3650 in B2 then change this line:

MyRow = GetRowNum("Db", "3650")

to this:

MyRow = GetRowNum("Db", [B2])


Unfortunately that didn't work...got Runtime Error 91: Object variable or With block variable not set :(
 
Upvote 0
Unfortunately that didn't work...got Runtime Error 91: Object variable or With block variable not set :(
Does cell B2 of sheet Db have just the number 3650 in it? If yes, the error suggests that 3650 is not found in any cell in col A of sheet Db, in part or in whole. Can you post a sample of your data in col A including a cell that you think has 3650 in it?
 
Upvote 0
Does cell B2 of sheet Db have just the number 3650 in it? If yes, the error suggests that 3650 is not found in any cell in col A of sheet Db, in part or in whole. Can you post a sample of your data in col A including a cell that you think has 3650 in it?

Apologies...thought I had edited my second post. Both solutions did work....THANK YOU!
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,182
Members
453,021
Latest member
Mohamed Magdi Tawfiq Emam

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