vba 'Match' function with dates

BigLRIP

Board Regular
Joined
May 7, 2009
Messages
123
Hi,

I'm trying to use the Match function to take a specific date and find it within a row of cells. However, dates are giving me problems as the match function seems to be functioning poorly.

Code:
...
Sheets("Sheet1").Select
date = Cells(row_index, 5).Text
lookup_range = Sheets("Sheet2").Range("2:2")
date_col_index = Application.Match(date, lookup_range, 0)
...

date_col_index returns 'Error 2042'

My dates are in the format of mmm-yy.
I was thinking about casting my date as a long, but the lookup_range would still be in normal date format. Any ideas around this?
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Don't use Date as a variable name - Date is the equivalent of Today() in VBA.

Once the above is corrected I would cast the variable to Long in the Match as you suggest.
 
Upvote 0
Don't use Date as a variable name - Date is the equivalent of Today() in VBA.

Once the above is corrected I would cast the variable to Long in the Match as you suggest.

I'm actually not using date as a variable name, just changed it for this forum.

Code:
Dim quarter As String
quarter = Sheets("Sheet1").Cells(row_index, 5).Text
quarter = CDate(quarter)
quarter = CLng(quarter)

Final line keeps giving me a <'type mismatch> error
 
Upvote 0
...quarter is declared as a string.

...accepting the above fact and adapting your earlier pseudo-code perhaps:

Code:
Dim quarter As String
quarter = Sheets("Sheet1").Cells(row_index, 5).Text
Set lookup_range = Sheets("Sheet2").Range("2:2")
date_col_index = Application.Match(CLng(CDate(quarter)), lookup_range, 0)
Set lookup_range = Nothing

It's unclear what date_col_index is declared as but it should probably be a Variant (test variable result IsNumeric to ensure valid Match has been found)
 
Upvote 0
...quarter is declared as a string.

...accepting the above fact and adapting your earlier pseudo-code perhaps:

Code:
Dim quarter As String
quarter = Sheets("Sheet1").Cells(row_index, 5).Text
Set lookup_range = Sheets("Sheet2").Range("2:2")
date_col_index = Application.Match(CLng(CDate(quarter)), lookup_range, 0)
Set lookup_range = Nothing

It's unclear what date_col_index is declared as but it should probably be a Variant (test IsNumeric to ensure valid Match has been found)

Thanks Donkey I'll test it out.

I actually just changed
Code:
quarter = Sheets("Sheet1").Cells(row_index, 5).Text
to:
Code:
quarter = Sheets("Sheet1").Cells(row_index, 5).Value

Then did
Code:
Cdate(quarter)

and it seems to be working fine.
 
Upvote 0

Forum statistics

Threads
1,224,514
Messages
6,179,217
Members
452,895
Latest member
BILLING GUY

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