Application.Match Error

nigelk

Well-known Member
Joined
Aug 30, 2008
Messages
537
Hi all, can anyone tell me why I get a "type mismatch" error when trying to run this?

Code:
RowNum = Application.Match("A. Smith", Sheets("sheet2").Range("a1:a500"), 0)

thanks,Nigel
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Hi Nigel,

Not sure, but this worked for me. Did you Dim your variable correctly?

Option Explicit
Code:
Sub test()
    Dim RowNum As Long
    Dim Rng As Range
    Set Rng = Sheets("Sheet2").Range("A1:A500")
    RowNum = Application.Match("A. Smith", Rng, 0)
    MsgBox RowNum
End Sub
 
Upvote 0
You probably need to place a quick test.

Create a new sheet and title it Sheet2.

In cell A7 put A. Smith

Place the following code in a standard module >> close the VBE >> press Alt + F8 >> run test

Msgbox should return 7

Code:
Sub test()
    Dim RowNum As Long
    Dim Rng As Range
    Dim strFind As String
    strFind = "A. Smith"
    Set Rng = Sheets("Sheet2").Range("A1:A500")
    RowNum = Application.Match(strFind, Rng, 0)
    MsgBox RowNum
End Sub
 
Upvote 0
Application.Match returns either a Double or error. As such, RowNum should be declared as a Variant type...

Code:
Dim RowNum as Variant
 
Upvote 0
Hi Domenic,

How come it worked when as Long was used or will it possibly run into an error at some point?
 
Upvote 0
Hi Domenic,

How come it worked when as Long was used or will it possibly run into an error at some point?

Hi Jeff,

Long cannot accept an error. Therefore, a type mismatch error occurs whenever Application.Match returns an error. Hence, the variable is declared as a Variant data type, which can accept errors along with other types of data.
 
Upvote 0

Forum statistics

Threads
1,224,587
Messages
6,179,735
Members
452,939
Latest member
WCrawford

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