As @Bruno Coimbra spoke of interpolation, it is not possible.
However it is possible to work with the date in format Long
and then convert it to Date
again.
The functions used are those of type conversion: CLng()
and CDate()
For example you can extract month and year:
mesAno= (Right(ThisWorkbook.Sheets(1).Range("D3"), 7)) ' mesAno = 08-2017
And extract the number of days of that month:
intDaysInMonth = day(DateSerial(Year(mesAno), Month(mesAno) + 1, 0))
Creating a loop with the number of days that month:
For i = 0 To intDaysInMonth - 1
End For
An example to look for a specific month and year:
Dim monthYear As Date
Dim rng As Range, rng2 As Range, cellFound As Range
Dim ws As Worksheet
Dim i As Long, lastrow As Long
Set ws = ThisWorkbook.Sheets(1)
lastrow = ws.Cells(ws.Rows.Count, "D").End(xlUp).Row
Set rng = ws.Range(ws.Cells(3, 4), ws.Cells(lastrow, 4))
mesAno= (Right(ThisWorkbook.Sheets(1).Range("D3"), 7)) ' mesAno = 08-2017
intDaysInMonth = day(DateSerial(Year(mesAno), Month(mesAno) + 1, 0))
For i = 0 To intDaysInMonth - 1
LookingFor = CLng(mesAno) + i
LookingForString = CStr(CDate(LookingFor))
With rng
Set cellFound = .Find(what:=LookingForString, LookIn:=xlValues, MatchCase:=False)
If Not cellFound Is Nothing Then
FirstAddress = cellFound.Address
Do
Debug.Print cellFound.Address
Set cellFound = .FindNext(cellFound)
Loop While Not cellFound Is Nothing And cellFound.Address <> FirstAddress
End If
End With
Next i
You first transform to Long and get an integer number that corresponds to each Date since 01/01/1900 and sum with the Loop of each day that month contains:
LookingFor = CLng(mesAno) + i
After that you turn to Date
with CDate
and then to String
with CStr
LookingForString = CStr(CDate(LookingFor))
It is extensive and above basic level, but to work with Dates you can work with them in Data Type or convert them to Long and work. For example, you tried to use a loop, so work in the format Long
should facilitate this.
is sure that the values of the variables
a
andb
are interpolated correctly? I don’t know enough of VBA, but it seems that this is the problem– Bruno Coimbra
Hello Bruno. Thank you so much for your reply and willingness to help me! What do you mean by interpolated correctly? The variable a is the month and b is the year... so when I do "01/a/b" it should see or read 01/7/2017... in case the date I was trying to read...
– CardosoI
Interpolation is exactly the substitution of variables inside the string. I’m not sure how this works in VBA, but I get the impression that
"01 / a / b"
will not fuck because the comparison will be with the literal valuesa
andb
. try something like:"01 / " & a & " / " & b
, to concatenate the values before comparing.– Bruno Coimbra
According to this answer in the OS, there is no interpolation of variables and the best path is concatenation of the same values. https://stackoverflow.com/questions/28221349/vba-string-interpolation-syntax
– Bruno Coimbra