Find function using variable

nora1212

New Member
Joined
Jan 29, 2021
Messages
15
Office Version
  1. 2010
Platform
  1. Windows
Hi,

I'm trying to use the find function in VBA, but need the "what" to reference a variable rather than the actual search text. In this instance the variable is called "ModuleNo" which contains the name "M123456". VBA doesn't appear to be able to use the variable in this way. Any ideas?

This works:
Range("A1:Z500").Find(What:="M123456", LookIn:=xlValues).Select

This doesn't
Range("A1:Z500").Find(What:=ModuleNo, LookIn:=xlValues).Select
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Works fine for me, what are your settings in the Find dialog?

1675690102531.png

Book1.xlsb
ABCDE
1
2
3
4
5
6
7
8
9
10
11M123456
12
Sheet5
 
Upvote 0
Solution
Hi Mark,

Thanks for your help. Rookie mistake. I didn't declare the variable :rolleyes:
 
Upvote 0
Hi Mark,

Thanks for your help. Rookie mistake. I didn't declare the variable :rolleyes:
Happy you have got it working but omitting the variable shouldn't make a difference (if you declared it wrong it would but not omitting it)
 
Upvote 0
Happy you have got it working but omitting the variable shouldn't make a difference (if you declared it wrong it would but not omitting it)
Yes, you're right....hmmm. It must have been a typo on my part and/or the number wasn't found. Thanks anyway though.
 
Upvote 0

Forum statistics

Threads
1,214,927
Messages
6,122,309
Members
449,080
Latest member
jmsotelo

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