Match error... but there should be a match

Luke777

Board Regular
Joined
Aug 10, 2020
Messages
246
Office Version
  1. 365
Platform
  1. Windows
Hi all,

I'm using the following code

VBA Code:
Sub test3()

Dim ws1 As Worksheet, ws2 As Worksheet
Dim promostartDateTime As Date
Dim SearchRow As Integer
Dim sTime As Double

Set ws1 = Worksheets(1)
Set ws2 = Worksheets(2)

promostartDateTime = Application.WorksheetFunction.Text(ws2.Range("A2"), "dd/mm/yyyy") & "  " & Application.WorksheetFunction.Text(ws2.Range("F2"), "hh:mm:ss") 'can ignore this line
sTime = CDate(promostartDateTime)
SearchRow = Application.Match(sTime, ws1.Range("A4:A148"), 0)

Debug.Print promostartDateTime; SearchRow; ws1.Range("A14").Value; sTime 'just looking to see what the values are

End Sub

debug.print (when commenting out SearchRow = to prevent error) shows: "24/01/2022 09:00:00 0 44585.375 44585.375"

This means that A14 in ws1 SHOULD match sTime? instead I'm given the debug type-mistmatch error 13 because it can't find a match.

Any ideas why it cannot match the value of sTime and A14 to give me the row number I'm looking for?

Thanks all :)
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
okay so changing the match type from 0 to 1 works... but I do not understand why type 0 doesn't or if this might have consequences with inaccurate matches later down the line
 
Upvote 0
unless there is A2 and F2 aren't a date and a time the construction to change both to a string and reverse is unnecessary and leads to error, as VBA follows the american dateformat.
Better is, i think stime=Ws2.range("A2").value2+Ws2.range("F2").value2
VBA Code:
promostartDateTime = Application.WorksheetFunction.Text(ws2.Range("A2"), "dd/mm/yyyy") & "  " & Application.WorksheetFunction.Text(ws2.Range("F2"), "hh:mm:ss")     'can ignore this line
sTime = CDate(promostartDateTime)
mydates = ws1.Range("A4:A148").Value2
SearchRow = Application.Match(sTime, mydates, 0)
 
Upvote 0
unless there is A2 and F2 aren't a date and a time the construction to change both to a string and reverse is unnecessary and leads to error, as VBA follows the american dateformat.
Better is, i think stime=Ws2.range("A2").value2+Ws2.range("F2").value2
VBA Code:
promostartDateTime = Application.WorksheetFunction.Text(ws2.Range("A2"), "dd/mm/yyyy") & "  " & Application.WorksheetFunction.Text(ws2.Range("F2"), "hh:mm:ss")     'can ignore this line
sTime = CDate(promostartDateTime)
mydates = ws1.Range("A4:A148").Value2
SearchRow = Application.Match(sTime, mydates, 0)
I'm getting type mistmatch with this - which I'm guessing has something to do with me using the uk date format?
 
Upvote 0
unless there is A2 and F2 aren't a date and a time the construction to change both to a string and reverse is unnecessary and leads to error, as VBA follows the american dateformat.
Better is, i think stime=Ws2.range("A2").value2+Ws2.range("F2").value2
VBA Code:
promostartDateTime = Application.WorksheetFunction.Text(ws2.Range("A2"), "dd/mm/yyyy") & "  " & Application.WorksheetFunction.Text(ws2.Range("F2"), "hh:mm:ss")     'can ignore this line
sTime = CDate(promostartDateTime)
mydates = ws1.Range("A4:A148").Value2
SearchRow = Application.Match(sTime, mydates, 0)
Just had another look - again works perfectly but only if the match_type is 1 instead of 0
 
Upvote 0
unless there is A2 and F2 aren't a date and a time the construction to change both to a string and reverse is unnecessary and leads to error, as VBA follows the american dateformat.
Better is, i think stime=Ws2.range("A2").value2+Ws2.range("F2").value2
VBA Code:
promostartDateTime = Application.WorksheetFunction.Text(ws2.Range("A2"), "dd/mm/yyyy") & "  " & Application.WorksheetFunction.Text(ws2.Range("F2"), "hh:mm:ss")     'can ignore this line
sTime = CDate(promostartDateTime)
mydates = ws1.Range("A4:A148").Value2
SearchRow = Application.Match(sTime, mydates, 0)
Sorry, last follow up - whats the best data type for mydates? or should I leave it without a dim?
 
Upvote 0
variant, but unnecessary to declare.
What is the difference between a2+f2 and the match, something very small ? (1e-15???)
 
Upvote 0
variant, but unnecessary to declare.
What is the difference between a2+f2 and the match, something very small ? (1e-15???)
I cant see any difference at all... they're both date value 44585.375 according to debug.print. Cell formatting should make any difference should it? e.g. on screen ws1.Range("A4:A148") are formatted as hh:mm but the actual value remains the same (and contains the full date that is used it other bits of code elsewhere that works fine) otherwise it wouldn't match at all would it?
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,159
Members
453,021
Latest member
Justyna P

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