Application.Match yielding "Type Mismatch"

wadevcamp

New Member
Joined
Jul 18, 2012
Messages
16
The following code results in a 'type mismatch" when trying to calculate 'i' in both cases. The range wrng2 is depicted below, and the values are Date type.

1576183367949.png


VBA Code:
Dim i As Integer
Dim d As Date
    d = #8/4/2019#
    i = Application.Match(d, Range("wrng2").Value, -1)
    d = Range("wrng2").Cells(7, 1).Value
    i = Application.Match(d, Range("wrng2").Value, -1)

Any help would be much appreciated.
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Change the declaration i as variant.

Dim i As Variant

Because that can receive a value or an error if it does not exist.
 
Upvote 0
The following code results in a 'type mismatch" when trying to calculate 'i' in both cases. The range wrng2 is depicted below, and the values are Date type.

View attachment 1942

VBA Code:
Dim i As Integer
Dim d As Date
    d = #8/4/2019#
    i = Application.Match(d, Range("wrng2").Value, -1)
    d = Range("wrng2").Cells(7, 1).Value
    i = Application.Match(d, Range("wrng2").Value, -1)

Any help would be much appreciated.
I
Change the declaration i as variant.

Dim i As Variant

Because that can receive a value or an error if it does not exist.
Ok, but the value DOES exist. It works if I change the last MATCH argument to '0', and if I make the wrng2 into an ascending range and use a match type of '0' or '1'. The only case that does not work is when the range is descending and the match type is '-1'.
 
Upvote 0
As you commented, the range must have the dates descending for type -1 to work

Check the following, in the code I put some comments.

VBA Code:
Sub test()
  Dim i As Variant, d As Date
  d = #8/24/2019#   'mm/dd/yyy
  'with Int(CDbl(d))  We get the excel number for a date
  i = Application.Match(Int(CDbl(d)), Range("wrng2"), -1)
End Sub
 
Last edited:
Upvote 0
As you commented, the range must have the dates descending for type -1 to work

Check the following, in the code I put some comments.

VBA Code:
Sub test()
  Dim i As Variant, d As Date
  d = #8/24/2019#   'mm/dd/yyy
  'with Int(CDbl(d))  We get the excel number for a date
  i = Application.Match(Int(CDbl(d)), Range("wrng2"), -1)
End Sub
Still results in a Type Mismatch (Date type and Double type appear to both be 8 byte floating point values.)
 
Upvote 0
Excel stores a date as an integer.

On the screen you can see the date with a format, but to perform searches you must use the integer number of the date and not the formatted date.
 
Upvote 0

Forum statistics

Threads
1,215,410
Messages
6,124,752
Members
449,186
Latest member
HBryant

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