VBA Index match of dates

pingme89

Board Regular
Joined
Jan 23, 2014
Messages
176
Office Version
  1. 365
Platform
  1. Windows
I am trying to take a date from a Userform field and compare it to Column "A". If there is a match, I want the row number to the row where the match was found.

For testing purposes these are the values in Column "A":

[TABLE="width: 68"]
<colgroup><col></colgroup><tbody>[TR]
[TD]#2/26/2018#[/TD]
[/TR]
[TR]
[TD]#2/25/2018#[/TD]
[/TR]
[TR]
[TD]#2/21/2018#[/TD]
[/TR]
[TR]
[TD]#2/22/2018#[/TD]
[/TR]
[TR]
[TD]#2/28/2018#
[/TD]
[/TR]
</tbody>[/TABLE]

I want to use Serial Dates to avoid potential issues of date formats. The problem I am getting is that I want to use Match with Type 0 to get an exact match. No matter what I do, I get an Error 2042. This is the code I have so far:

Dim wbname As String
Dim DT As Date
DT = CDate(txtDate)


wbname = ThisWorkbook.Name
RowCount = Workbooks(wbname).Worksheets("Course Data").UsedRange.Rows.Count
ColCount = Workbooks(wbname).Worksheets("Course Data").UsedRange.Columns.Count


ColLett = Split(Cells(1, ColCount).Address, "$")(1)
RangeName = "ScheduleData"
CellName = "A1:" & ColLett & RowCount + 1
Set cell = Workbooks(wbname).Worksheets("Course Data").Range(CellName)
ThisWorkbook.Names.Add Name:=RangeName, RefersTo:=cell


ColLett = Split(Cells(1, 1).Address, "$")(1)
RangeName = "Dates"
CellName = "A1:" & ColLett & RowCount + 1
Set cell = Workbooks(wbname).Worksheets("Course Data").Range(CellName)
ThisWorkbook.Names.Add Name:=RangeName, RefersTo:=cell


Set Dates = Workbooks(wbname).Names("Dates").RefersToRange
Set ScheduleData = Workbooks(wbname).Names("ScheduleData").RefersToRange


Variable = Application.Match(DT, Dates, 0)
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Hi

The worksheet does not have the type date.

Try:

Code:
Dim l As Long
l = Application.Match(CLng(DT), Dates, 0)

Remark: I assumed it's a date value, if it's a datetime value use instead CDbl()
 
Upvote 0
Now I get a type mismatch error.
Run-Time Error 13.

The value of CLng(DT) now = 43159
The value of DT = 2/28/18
 
Upvote 0
Hi

That's usually when the value is not found.
Maybe the values in Dates range are not date values?

This simple test will maybe help you figure out the problem.

The code writes 3 dates in A1:A3 and then searches the second date in the range.

Try:

Code:
Sub test()
Dim dt As Date

Range("A1:A3").NumberFormat = "General"
Range("A1").Value = DateSerial(2018, 2, 1)
Range("A2").Value = DateSerial(2018, 2, 2)
Range("A3").Value = DateSerial(2018, 2, 3)

dt = DateSerial(2018, 2, 2)
MsgBox Application.Match(CLng(dt), Range("A1:A3"), 0)

End Sub

You should get a box with the message "2"
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,197
Members
453,022
Latest member
RobertV1609

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