Match Function use in VBA

EssoExplJoe

New Member
Joined
Nov 11, 2016
Messages
28
I am trying to use the Match funcition in VBA but always get an error: Below is my code

VBA Code:
dim ctable as range, wstab as worksheet
Set wstab = Worksheets("CUSPIDTable")
Set ctable = Range(wstab.Cells(4, 6), wstab.Cells(100, 6))
row = Application.Match("34567923", ctable.Address, 0)

The Match always returns an error if or not if the value "34567923" is in the range. I have tried just using "ctable" without the .address but it doesn't work either.

What is the proper syntax for using a named range in the match function
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Found a syntax that works after a couple of hours trying different things. There may be a more elegant solution but the below works:

VBA Code:
row = Application.Match(cuspid, wstab.Range(ctable.Address), 0)
 
Upvote 0
Solution
Hi.
If the values in the "ctable" range contain numbers and not numbers formatted as text, then the command below should work.
The searched value should be without double quotes.

VBA Code:
lngRow = Application.Match(34567923, ctable, 0)
 
Upvote 0
What I ended up doing was assigning the item to be match to a Variant type variable then it doesn't matter if the value to be matched is a number or a text. Thks for pointing this out. Final code I use is:
 
Upvote 0
What I ended up doing was assigning the item to be match to a Variant type variable then it doesn't matter if the value to be matched is a number or a text. Thks for pointing this out. Final code I use is:

VBA Code:
dim ctable as range, wstab as worksheet, matchvalue as variant
Set wstab = Worksheets("CUSPIDTable")
Set ctable = Range(wstab.Cells(4, 6), wstab.Cells(100, 6))
matchvalue = "34567923"
row = Application.Match("34567923", ctable.Address, 0)  'this works
matchvalue = 34567923
row = Application.Match("34567923", ctable.Address, 0)  ''this works also
 
Upvote 0

Forum statistics

Threads
1,214,657
Messages
6,120,769
Members
448,991
Latest member
Hanakoro

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