I receive a workbook twice a month that contains information about the processing volumes of a call center team. I have no way of modifying the format or layout of the workb
@KOstvall,, I would like to suggest this simple find method, since you are trying to get the Date.
Set rng = Sheet1.Range("A:A").Find("1/1/2017", LookIn:=xlValue, LookAt:=xlWhole)
Dates are tricky to find with the Range.Find
method. One of the issues is that in VBA, dates are of the Date
data type, but the worksheet does not have that data type. Rather the data type is a number that is formatted to look like a date.
One solution, if you can be certain of the format of the date on the worksheet, is to search for the string equivalent. Given your example, something like this will work:
Option Explicit
Sub GetDates()
Const findDate As Date = #5/11/2017#
Dim findStr As String
Dim R As Range, WS As Worksheet
Set WS = Worksheets("Sheet1")
findStr = Format(findDate, "dd-mmm")
With WS
Set R = .Cells.Find(what:=findStr, LookIn:=xlValues, lookat:=xlWhole)
If Not R Is Nothing Then MsgBox findDate & " found in " & R.Address
End With
End Sub
but it is not very robust since, in many cases, the user can change the format.
Another method that is more robust, would be to loop through the existing cells, looking for the numeric representation of the date (using the Value2
property):
Sub GetDates2()
Const findDate As Date = #5/11/2017#
Dim R As Range, C As Range, WS As Worksheet
Set WS = Worksheets("sheet1")
Set R = WS.UsedRange
For Each C In R
If C.Value2 = CDbl(findDate) Then MsgBox findDate & " found in " & C.Address
Next C
End Sub
If you have a large range to search, this can be sped up by a factor of ten by reading the range into a VBA array and looping through the array.
Been facing the same issue when using .find
for dates. Even if the Target Date Format for search is in dd-mmm
Use .find with d-mmm
format.
Date_Value = Application.Text(Date_Value, "d-mmm-yy") 'Use Format for your case
Set r_gt= .Range(EODate_Range).Find(Date_Value, LookIn:=xlValues)
PS: If the target date format is dd-mm-yy
, you'll have to loop through the cells.
Tried the above code for 01-01-20
after changing search format to d-mm-yy
/ d-m-yy
and it failed to find the date, but it finds the date if cell format is changed to 01-Jan-20
.
Update: I've modified the code below to set the active cell before the find. The second animated gif show the code running
One approach is to do a find based upon the specific date format, which in your case appears to be "[$-en-US]dd-mmm;@"
. The following is a simple example, which you could then adjust to your need. To make this example work, place 3 dates into "A1:A3", with just one of them having the desired format, and then run the macro. The animated gif shows how to set the format.
Sub dateFormatFind()
Dim sh As Worksheet, searchR As Range
Dim cell As Range, resultR As Range
Set sh = Worksheets("Sheet5")
Set searchR = sh.Range("A1:A3")
Set resultR = sh.Range("C1")
sh.Range("A1").Activate
Application.FindFormat.NumberFormat = "[$-en-US]dd-mmm;@"
resultR = searchR.Find(What:="", After:=ActiveCell, _
LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=True)
End Sub