Date issues when using .Find in VBA (UK to US conversion)

JackDanIce

Well-known Member
Joined
Feb 3, 2010
Messages
9,922
Office Version
  1. 365
Platform
  1. Windows
Hi,

I have a spreadsheet, where cells that will contain date values are already formatted to be UK style.

Using VBA, I search each row of date values to find a matching date and return the column number.

The search variable and intermediate window correctly shows the date. E.g. I'm searching for 26/04/2017, when I used debug.print arr(x,1) it shows as 26/04/2017

The range being searched contains this date and cell is showing it as 26/04/2017, again checking formatting, it's set to UK style.

I run the following function to return an array with column index numbers to matched date values.
Code:
Private Function ColNum(ByRef arr() As Variant, ByRef xRow As Long) As Variant

    Dim rngDate     As Range
    Dim LC          As Long
    Dim x           As Long
    Dim tempDate    As Date
        
    With Sheets("OR Sheet")
        LC = LastCol(Sheets("OR Sheet"), xRow) - 6
        If LC > 5 Then
            Set rngDate = .Cells(xRow, 7).Resize(, LC)
            Debug.Print rngDate.Address
            For x = LBound(arr, 2) To UBound(arr, 2)
                If IsDate(arr(1, x)) Then
                    On Error Resume Next
                    arr(1, x) = CLng(rngDate.Find(arr(1, x), , xlValues, xlWhole, xlByColumns).Column)
                    On Error GoTo 0
                    If Not IsNumeric(arr(1, x)) Then arr(1, x) = Empty
                End If
            Next x
            Set rngDate = Nothing
        End If
    End With
    
    ColNum = arr
    
End Function
When I print the array contents, all elements are empty with no column index values, EXCEPT when day = month (they have same values as each other).

Next, I press CTRL+F on the spreadsheet and the Find dialogue box is showing the last searched item as 4/26/2017 so somewhere/how using .Find through VBA, Excel is searching for 4/26/2017 which of course does not exist in the range; it appears to have swapped the month with the day and then treated it as a US date.

Anyone experienced this before and suggestions to resolve?

Thanks in advance,
Jack
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Jack

Have you tried using Match instead of Find?
Code:
arr(1, x) = Application.Match(arr(1,x), rngDate.EntireRow, 0)

PS How are you populating the array?
 
Upvote 0
Hi Norrie,

No not tried using MATCH. The array is reading dates from sheet OR and then matching them to different sheet to find column index numbers.

Both sheets have cell format set to date (UK) where dates are found. I'll try Match, out of curiosity, any reason Match would work and .Find not?

Will report back with results.

Cheers,
Jack
 
Upvote 0
Jack

No particular reason, just seems to me, from experience anyway, that using Match for this sort of thing is more succesful than Find - lot simpler too.

Obviously if you are looking for partial matches, values in formulas etc Match wouldn't be much use.:)
 
Upvote 0
Hi Norrie,

Annoyingly, MATCH works when I use it in the spreadsheet, but not in the code as suggested. I've omitted .Entirerow because I have another function LastCol that defines the last used column per row, I cannot see how that could be the fix to this...

Comments?
Jack
 
Upvote 0
Jack

How is it not working in the code?

PS You need to use EntireRow to get the correct column, otherwise if there is a match you'll get the column within rngDate.

For example if the date you were looking for was found in the 3rd column of rngDate you would get 3, not 10.
 
Upvote 0
Hi Norrie,

I need relative position of column so .Entirerow is unnecessary, but appreciate the reasoning - all the data on all relevant sheets is anchored to column 7.

When I had .Find in the code and using F5 to step through the code, after these lines, I found the following:
Code:
On Error Resume Next
arr(1, x) = rngDate.Find(arr(1, x), , xlValues, xlWhole, xlByColumns).Column
On Error GoTo 0
arr(1, x) still contains #26/04/2017# and I verified this in the intermediate window with debug.print
Pressing CTRL+F on the spreadhseet pulls up the Find dialogue window and it's showing Find what:=4/26/2017

When I tried Application.Match, it would return Error4042 to arr(1, x)

I believe despite it holding a UK formatted date, it is converting from code to execution into a US style and then trying to find a US date in a UK range, so only when day number = month number does it successfully return the column index number.

This PC is set to UK time/date etc, so is Excel (using 2016)
 
Last edited:
Upvote 0
Jack,

This worked for me (Regional Setting for dates dd/mm/yyyy)

OR Sheet

[TABLE="class: grid"]
<tbody>[TR]
[TD="bgcolor: #DCE6F1"][/TD]
[TD="bgcolor: #DCE6F1"]
A
[/TD]
[TD="bgcolor: #DCE6F1"]
B
[/TD]
[TD="bgcolor: #DCE6F1"]
C
[/TD]
[TD="bgcolor: #DCE6F1"]
D
[/TD]
[TD="bgcolor: #DCE6F1"]
E
[/TD]
[TD="bgcolor: #DCE6F1"]
F
[/TD]
[TD="bgcolor: #DCE6F1"]
G
[/TD]
[TD="bgcolor: #DCE6F1"]
H
[/TD]
[TD="bgcolor: #DCE6F1"]
I
[/TD]
[TD="bgcolor: #DCE6F1"]
J
[/TD]
[TD="bgcolor: #DCE6F1"]
K
[/TD]
[TD="bgcolor: #DCE6F1"]
L
[/TD]
[TD="bgcolor: #DCE6F1"]
M
[/TD]
[TD="bgcolor: #DCE6F1"]
N
[/TD]
[TD="bgcolor: #DCE6F1"]
O
[/TD]
[TD="bgcolor: #DCE6F1"]
P
[/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
1
[/TD]
[TD]
SearchD1​
[/TD]
[TD]
SearchD2​
[/TD]
[TD]
SearchD3​
[/TD]
[TD]
SearchD4​
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
2
[/TD]
[TD]
26/04/2017​
[/TD]
[TD]
22/04/2017​
[/TD]
[TD]
29/04/2017​
[/TD]
[TD]
01/05/2017​
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
3
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
4
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
5
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
6
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]
Date1​
[/TD]
[TD]
Date2​
[/TD]
[TD]
Date3​
[/TD]
[TD]
Date4​
[/TD]
[TD]
Date5​
[/TD]
[TD]
Date6​
[/TD]
[TD]
Date7​
[/TD]
[TD]
Date8​
[/TD]
[TD]
Date9​
[/TD]
[TD][/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
7
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]
22/04/2017​
[/TD]
[TD]
23/04/2017​
[/TD]
[TD]
24/04/2017​
[/TD]
[TD]
25/04/2017​
[/TD]
[TD]
26/04/2017​
[/TD]
[TD]
27/04/2017​
[/TD]
[TD]
28/04/2017​
[/TD]
[TD]
29/04/2017​
[/TD]
[TD]
30/04/2017​
[/TD]
[TD][/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
8
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
9
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]


Code:
Option Explicit
Sub Main()
    Dim vDates(1 To 1, 1 To 4) As Variant, myArr As Variant
    Dim i As Long
    
    With Sheets("Or Sheet")
        vDates(1, 1) = .Range("A2")
        vDates(1, 2) = .Range("B2")
        vDates(1, 3) = .Range("C2")
        vDates(1, 4) = .Range("D2")
    End With
    
    myArr = ColNum(vDates, 7)
    For i = LBound(myArr, 2) To UBound(myArr, 2)
        Debug.Print myArr(1, i)
    Next i
End Sub


Private Function ColNum(ByRef arr As Variant, ByRef xRow As Long) As Variant
    Dim rngDate     As Range
    Dim x           As Long
    Dim rFound      As Range
        
    With Sheets("OR Sheet")
         Set rngDate = .Cells(xRow, 7).Resize(, 9)
         For x = LBound(arr, 2) To UBound(arr, 2)
            Set rFound = rngDate.Find(arr(1, x), , xlValues, xlWhole, xlByColumns)
            If Not rFound Is Nothing Then
                arr(1, x) = rFound.Column
            Else
                arr(1, x) = "Not Found"
            End If
        Next x
    End With
    ColNum = arr
End Function

I got these results (Debug.print)
11
7
14
Not Found

Hope this helps

M.
 
Upvote 0
If you need the relative position you can use

Code:
If Not rFound Is Nothing Then
    arr(1, x) = rFound.Column [COLOR=#ff0000]- 6[/COLOR] 'or something like that
Else
    arr(1, x) = "Not Found"
End If

M.
 
Last edited:
Upvote 0
Hey Marcelo, thanks for the replies. I'll try the check against client's sheet tomorrow and update... it's very odd!

Relative position isn't issue, though appreciate the suggestion; just want to return the column number however it is!

As mentioned the intermediate window shows the array element contents as #26/04/2017#, but after I step through the code and press CTRL+F on sheet, the find dialogue box is showing last searched item being "4/26/2017" in the input section which is what's making me believe it's an internal issue after the code is compiled and executed...
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,194
Members
453,021
Latest member
pingpong7117

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