SOLVED

Finding a date on another sheet.

%3CLINGO-SUB%20id%3D%22lingo-sub-2555220%22%20slang%3D%22en-US%22%3EFinding%20a%20date%20on%20another%20sheet.%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2555220%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20nice%20people%2C%3C%2FP%3E%3CP%3EIn%20my%20application%20I%20compute%20a%20variable%20called%20EntryDate%20(of%20data%20type%20date)%20from%20a%20form%20with%20DateSerial(txtYear%2CtxtMonth%2CtxtDay).%20This%20(using%20an%20example)%20produces%2001%2F04%2F2021%2C%20which%20is%20correct.%20It%20is%20then%20asked%20to%20see%20if%20this%20date%20is%20present%20on%20another%20sheet%2C%20using%3A%3C%2FP%3E%3CP%3EWith%20Worksheets(%22OtherSheet%22).Range(%22WorkRange%22)%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%20Set%20MyEdit%20%3D%20.Find(EntryDate%2C%20%2C%20xlValues%2C%20xlWhole%2C%20xlByColumns%2C%20%2C%20True)%3C%2FP%3E%3CP%3EEnd%20With%3C%2FP%3E%3CP%3EIt%20does%20%3CSTRONG%3Enot%3C%2FSTRONG%3E%20find%20it.%20It%20%3CSTRONG%3Eshould%3C%2FSTRONG%3E%20because%20this%20date%20is%20present%2C%20in%20the%20first%20column%20(it%20will%20always%20be%20the%20first%20column%20as%20this%20is%20the%20date%20column).%3C%2FP%3E%3CP%3EI%20have%20used%20little%20test%20things%20like%20computing%20EntryDate%20as%20above%20and%20asking%20if%20this%20is%20equal%20to%20cell%20A2%20and%20the%20answer%20is%20yes%2C%20true.%20I%20can%20show%20EntryDate%3D%2001%2F04%2F2021%20and%20cell%20A2%20%3D%2001%2F04%2F2021%20although%20actually%20cell%20A2%20is%20formatted%20as%2001-04-2021.%3C%2FP%3E%3CP%3EI%20feel%20my%20problem%20may%20be%20related%20to%20date%20formatting.%3C%2FP%3E%3CP%3EClever%20people%2C%20I%20would%20so%20like%20to%20know%20why%20the%20computer%20knows%20that%20A2%20contains%20EntryDate%20but%20will%20not%20%22find%22%20it%20for%20me!%3C%2FP%3E%3CP%3EThis%20is%20part%20of%20code%20I%20am%20writing%20to%20avoid%20adding%20accidental%20duplicates.%3C%2FP%3E%3CP%3ESue%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2555220%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EMacros%20and%20VBA%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2555573%22%20slang%3D%22en-US%22%3ERe%3A%20Finding%20a%20date%20on%20another%20sheet.%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2555573%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F140105%22%20target%3D%22_blank%22%3E%40Sue%20Cuthbertson%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThis%20is%20a%20known%20problem.%20Excel%20VBA%20is%20very%20finicky%20when%20it%20comes%20to%20finding%20a%20date%20in%20a%20range.%3C%2FP%3E%0A%3CP%3ETwo%20workarounds%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E1)%20If%20the%20dates%20in%20WorkRange%20are%20constants%2C%20not%20the%20result%20of%20formulas%2C%20change%20xlValues%20to%20xlFormulas%3A%3C%2FP%3E%0A%3CP%3ESet%20MyEdit%20%3D%20.Find(EntryDate%2C%20%2C%20xlFormulas%2C%20xlWhole%2C%20xlByColumns%2C%20%2C%20True)%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E-%20or%20-%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E2)%20Leave%20xlValues%20alone%2C%20and%20specify%20the%20first%20argument%20formatted%20exactly%20as%20on%20the%20worksheet%3A%3C%2FP%3E%0A%3CP%3ESet%20MyEdit%20%3D%20.Find(Format(EntryDate%2C%20%22dd-mm-yyyy%22)%2C%20%2C%20xlValues%2C%20xlWhole%2C%20xlByColumns%2C%20%2C%20True)%3C%2FP%3E%0A%3CP%3Eor%3C%2FP%3E%0A%3CP%3ESet%20MyEdit%20%3D%20.Find(Format(EntryDate%2C%20%22mm-dd-yyyy%22)%2C%20%2C%20xlValues%2C%20xlWhole%2C%20xlByColumns%2C%20%2C%20True)%3C%2FP%3E%0A%3CP%3Edepending%20on%20which%20format%20the%20cells%20use.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2555614%22%20slang%3D%22en-US%22%3ERe%3A%20Finding%20a%20date%20on%20another%20sheet.%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2555614%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F140105%22%20target%3D%22_blank%22%3E%40Sue%20Cuthbertson%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22VIiyi%22%3E%3CSPAN%20class%3D%22JLqJ4b%20ChMk0b%22%3E%3CSPAN%3EHere%20is%20an%20example%20with%20VBA.%3C%2FSPAN%3E%3C%2FSPAN%3E%20%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22VIiyi%22%3E%3CSPAN%20class%3D%22JLqJ4b%20ChMk0b%22%3E%3CSPAN%3ESearch%20simple%20and%20effective.%3C%2FSPAN%3E%3C%2FSPAN%3E%20%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22VIiyi%22%3E%3CSPAN%20class%3D%22JLqJ4b%20ChMk0b%22%3E%3CSPAN%3EExample%20file%20and%20VBA%20code%20inserted.%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22VIiyi%22%3E%3CSPAN%20class%3D%22JLqJ4b%20ChMk0b%22%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3E'Insert%20into%20a%20module%0AOption%20Explicit%0ASub%20Search()%0ADim%20booGefunden%20As%20Boolean%0ADim%20strEingabe%20As%20String%0ADim%20rngFeld%20As%20Range%0ADim%20wksSheet%20As%20Worksheet%0AstrEingabe%20%3D%20Application.InputBox(%22Please%20enter%20search%20term%22%2C%20%22Search%22%2C%20%22Search%20term%22)%0AIf%20strEingabe%20%26lt%3B%26gt%3B%20%22%22%20Then%0A%20%20%20%20For%20Each%20wksSheet%20In%20ActiveWorkbook.Worksheets%0A%20%20%20%20%20%20%20%20For%20Each%20rngFeld%20In%20wksSheet.UsedRange%0A%20%20%20%20%20%20%20%20%20%20%20%20If%20InStr(UCase(CStr(rngFeld.Value))%2C%20UCase(strEingabe))%20%26gt%3B%200%20Then%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20booGefunden%20%3D%20True%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20wksSheet.Activate%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20rngFeld.Select%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20If%20MsgBox(%22Keep%20searching%3F%22%2C%20vbQuestion%20%2B%20vbYesNo)%20%3D%20vbNo%20Then%20Exit%20Sub%0A%20%20%20%20%20%20%20%20%20%20%20%20End%20If%0A%20%20%20%20%20%20%20%20Next%20rngFeld%0A%20%20%20%20Next%20wksSheet%0A%20%20%20%20If%20booGefunden%20%3D%20False%20Then%20MsgBox%20%22'%22%20%26amp%3B%20strEingabe%20%26amp%3B%20%22'%20not%20found!%22%2C%20vbInformation%0AEnd%20If%0AEnd%20Sub%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20would%20be%20happy%20to%20know%20if%20I%20could%20help.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ENikolinoDE%3C%2FP%3E%3CP%3EI%20know%20I%20don't%20know%20anything%20(Socrates)%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E*%20Kindly%20Mark%20and%20Vote%20any%20reply%20if%20it%20helps%20please%2C%20as%20it%20will%20be%20beneficial%20to%20more%20Community%20members%20reading%20here.%3C%2FP%3E%3C%2FLINGO-BODY%3E
Occasional Contributor

Hi nice people,

In my application I compute a variable called EntryDate (of data type date) from a form with DateSerial(txtYear,txtMonth,txtDay). This (using an example) produces 01/04/2021, which is correct. It is then asked to see if this date is present on another sheet, using:

With Worksheets("OtherSheet").Range("WorkRange")

   Set MyEdit = .Find(EntryDate, , xlValues, xlWhole, xlByColumns, , True)

End With

It does not find it. It should because this date is present, in the first column (it will always be the first column as this is the date column).

I have used little test things like computing EntryDate as above and asking if this is equal to cell A2 and the answer is yes, true. I can show EntryDate= 01/04/2021 and cell A2 = 01/04/2021 although actually cell A2 is formatted as 01-04-2021.

I feel my problem may be related to date formatting.

Clever people, I would so like to know why the computer knows that A2 contains EntryDate but will not "find" it for me!

This is part of code I am writing to avoid adding accidental duplicates.

Sue

3 Replies
best response confirmed by Sue Cuthbertson (Occasional Contributor)
Solution

@Sue Cuthbertson 

This is a known problem. Excel VBA is very finicky when it comes to finding a date in a range.

Two workarounds:

 

1) If the dates in WorkRange are constants, not the result of formulas, change xlValues to xlFormulas:

Set MyEdit = .Find(EntryDate, , xlFormulas, xlWhole, xlByColumns, , True)

 

- or -

 

2) Leave xlValues alone, and specify the first argument formatted exactly as on the worksheet:

Set MyEdit = .Find(Format(EntryDate, "dd-mm-yyyy"), , xlValues, xlWhole, xlByColumns, , True)

or

Set MyEdit = .Find(Format(EntryDate, "mm-dd-yyyy"), , xlValues, xlWhole, xlByColumns, , True)

depending on which format the cells use.

@Sue Cuthbertson 

Here is an example with VBA.

Search simple and effective.

Example file and VBA code inserted.

 

 

'Insert into a module
Option Explicit
Sub Search()
Dim booGefunden As Boolean
Dim strEingabe As String
Dim rngFeld As Range
Dim wksSheet As Worksheet
strEingabe = Application.InputBox("Please enter search term", "Search", "Search term")
If strEingabe <> "" Then
    For Each wksSheet In ActiveWorkbook.Worksheets
        For Each rngFeld In wksSheet.UsedRange
            If InStr(UCase(CStr(rngFeld.Value)), UCase(strEingabe)) > 0 Then
                booGefunden = True
                wksSheet.Activate
                rngFeld.Select
                If MsgBox("Keep searching?", vbQuestion + vbYesNo) = vbNo Then Exit Sub
            End If
        Next rngFeld
    Next wksSheet
    If booGefunden = False Then MsgBox "'" & strEingabe & "' not found!", vbInformation
End If
End Sub

 

 

I would be happy to know if I could help.

 

NikolinoDE

I know I don't know anything (Socrates)

 

* Kindly Mark and Vote any reply if it helps please, as it will be beneficial to more Community members reading here.

@Hans Vogelaar 

 

Hi Hans

Thank you very much. I have tried both your suggestions and both find the required date in the other sheet when it is present - so I will be able to proceed with the programming.

 

I have also learnt something new. From the vba help on ".Find" I had no idea that the "LookIn" field given as "The type of information" (which did not mean anything in particular to me) could include such terms as xlFormulas which could equate different date formats! Sometimes the standard help facility just isn't helpful, or helpful enough!

 

I congratulate you on how clear and concise your help was to my problem and also helpful in understanding issues with dates in general.

 

Sue