Using Match with Worksheetfunction

svjack9

New Member
Joined
Feb 9, 2014
Messages
32
Hello,
I'm having a problem using worksheetfunction.match in my vba code. The problem lines look like this:

set ws = activeworkbook.worksheets("Storage")
ws.activate
intColumnNumber=application.worksheetfunction.match("Crop","A1:Z1",0)

I'm getting error 1004: "Unable to get the match property of the worksheetfunction class."

I've tried dimming intColumnNumber as type double without success. Any ideas?

Thanks in advance,
svjack9
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Try
Code:
intColumnNumber=application.worksheetfunction.match("Crop",Range("A1:Z1"),0)

or better
Code:
intColumnNumber=application.match("Crop",Range("A1:Z1"),0)

Match needs a range, not a string, hence the Range(...) in both.

Excel has a quirk, if WorksheetFunction.Match doesn't find a match, it will cause a VB runtime error.
If Application.Match doesn't find a match, it will return the error value CVErr(xlErrNA), which makes for easier error checking.

Code:
Dim matchResult as Variant

matchResult = Application.Match("cat", Range("A:A"), 0)

If IsError(matchResult) Then
    MsgBox "cat is not in column A"
Else
    MsgBox "cat is in row " & matchResult & " of column A"
End If
 
Upvote 0

Forum statistics

Threads
1,214,563
Messages
6,120,248
Members
448,952
Latest member
kjurney

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