I'm trying to find in a row the date of today (I'm using VBA Date to get today's date) But Application.Match doesn't find anything.
The code I'm using is this:
Debug.Print Application.WorksheetFunction.Match(Date, Range("5:5"), 0)
The dates in row 5 are generated by formulas, so I have to search the values.
It really depends on how you are passing the date. In general, in .Match() the best way is to look for a specific numeric:
Public Sub TestMe()
Range("E5") = Clng(Date)
Debug.Print Application.Match(Clng(Date), Range("5:5"), 0)
Range("E5") = ""
Range("C5") = Clng(Date)
Debug.Print Application.Match(Clng(Date), Range("5:5"), 0)
End Sub
Sometimes the date system in Excel are behaving a bit strangely:
VBA treating dates differently in Excel 2016? Is there any documentation about this?
Range.Find not making a difference between January and November (February and December) in VBA Excel
A bit more about Dates in Excel (joelonsoftware.com)
Convert your date to Long:
Debug.Print Application.WorksheetFunction.Match(CLng(Date), Range("5:5"), 0)
You can't use MATCH() with a VBA Date like this. Instead use a Long:
Sub INeedADate()
MsgBox Application.WorksheetFunction.Match(CLng(Date), Range("5:5"), 0)
End Sub
The above is an example of both the worksheet use of MATCH() and the VBA use.
Don't use Match() for comparing dates, it's only for strings.
You can use the functions Year(), Month() and Day(): if year, month and day are equal, then the date is equal.
Related
I have a string of a date in french in long format like so:
mardi 7 juillet 2020
How can I convert it to a date type in VBA ? I tried using CDate, DateValue and DateSerial, but I couldn't figure it out. There must be a way if VBA has a Long Date format for dates. I just can't find anyone that was asking this conversion question with this format.
Note that I have a lot of dates since I am looping over many mails so I need a solution that is very general so a solution that takes into account all possible week days (lundi, mardi, ..., dimanche), all possible days 1 to 31 and months Janvier to Décembre and all possible years.
Thank you for your help.
I agree there should be an easier conversion. This function will strip out the weekday (which is superfluous) and then rely on VBA type conversion to get a Date type (NB. I have only tested this under a English(UK) regional setting).
Option Explicit
Public Function DateFromLongFormat(strLongFormatDate As String) As Date
Dim strDate As String
strDate = Right(strLongFormatDate, Len(strLongFormatDate) - InStr(1, strLongFormatDate, " "))
DateFromLongFormat = strDate
End Function
Tried it out using a simple test routine on today's date:
Sub TestDate()
Dim strToday As String
strToday = Format(Now(), "dddd dd mmmm yyyy")
Dim dt As Date
dt = DateFromLongFormat(strToday)
End Sub
I think that I try to do something what is impossible. I have in range A1:A5 some dates. These cells have to have date format, because I’d like to check if they are bigger or smaller than certain date. Moreover I’d like to display these dates in a range A1:A5 in a following format: yyyy.mm. I think that there is no way to kill two birds with one stone.
Public Sub test()
Range("A1:A5").NumberFormat = "yyyy.mm"
Range("A1:A5") = Format(Date, "yyyy.mm")
If IsDate("A2") Then
Range("B2") = "OK"
End If
End Sub
Range("B1:B5").NumberFormat = "yyyy.mm" change the cells format on custom (yyyy.mm) but the dates are still displayed in unchanged form (yyyy.mm.dd)
Range (“A1:A5”) = Format(Date, “yyyy.dd”) displays the dates yyyy.mm but the year and month has been changed to 1905.07. Moreover the dates do not have Date format but Custom.
Is is really not possible to display a date like yyyy.mm but in date format (so that in formular bar the date is displayed as dd.mm.yyyy or another way around) to be able to compare them with different dates?
Best regards,
Neke
First of all, using points within dates can be a problem, because "2017.05" is interpreted by Excel as a number.
You can reformat the dates into "yyyy/mm" by using something like this:
For Each r In Range("A1:A5")
r.Value = VBA.Format(r, "yyyy/mm")
Next r
I have a textbox wherein I input number as months (like "01" for January) but I need the actual month name instead of the number in my VBA statements. Can you please help me convert the number in the textbox into Actual Month Name.
I have tried the statement below but it is not working:
Dim MoName As String
MoName = Format(Month(Monthtxtbx.Text), "MMMM")
Range("Cells(1,1):Cells(LastCol, Lastcolumn)").AutoFilter Field:=3, Criteria1:=MoName
Use MoName = MonthName(Monthtxtbx.Text) to get the month name.
Alternatively, you could dummy up a date so that you could use the "mmmm" format as:
MoName = Format(DateSerial(2000,Monthtxtbx.Text,1), "MMMM")
but that would be the long way to achieve something simple.
How do we compare two date strings in vba, like "01.02.2013 < 02/02/2013"?
Whatever the dates are this always showing true. And two date formats are correct in the example i menioned.
Below vba code throws error.
Sub aa()
Dim a As Variant, b As Variant, c As Variant
a = Format("1.2.2012", "DD\/MM\/YYYY")
b = Format("2.2.2012", "DD\/MM\/YYYY")
MsgBox (a)
End Sub
Convert your dates (strings) to a format CDate() accepts. 02/02/2013 works, I think 02.02.2013 doesn't. Use Replace() if needed.
Then you can cast your String data into the Date datatype with CDate(myString). Dates can be compared with each other by the means of the usual operators, such as > < =.
Clean up the strings to the correct format (see what CDATE accepts, and then use CDate(a) >= CDate(b) to compare.
I have an Excel workbook for tracking backup success. We have a number of sheets with the date in the first column, and macros to do calculations, based on whether the date is in the past or not. (The macros either hide or reveal the appropriate rows.)
This has been working until yesterday. I assume because the macros are doing a string comparison, rather than a date comparison. ("01/01/2013" is smaller than "12/31/2012", when viewed as strings.)
Is there a native way to compare dates in VBA, or do I need to convert the dates into "yyyy/mm/dd" first (a how to would be nice).
A2 is the cell with the first date we started using this new version of the spreadsheet, and A454 is the last date I extended the spreadsheet to, corresponding to the end of this year.
Sub ShowAll()
Dim cell As Range
For Each cell In Range("A2:A454")
cell.EntireRow.Hidden = False
Next
End Sub
Sub RevealPast()
Dim cell As Range
For Each cell In Range("A2:A454")
If cell.Value < Date Then
cell.EntireRow.Hidden = False
End If
Next
End Sub
Sub HideFuture()
Dim cell As Range
For Each cell In Range("A2:A454")
If cell.Value >= Date Then
cell.EntireRow.Hidden = True
End If
Next
End Sub
Try the cDate function.
something along the lines of:
If CDate("02/01/2013") > Date (or Now() if you want today's date) Then
...
So, in your example:
If cDate(cell.Value) >= Date Then
I hope I understand your question correctly and hope this helps...
I know this is a bit different from what you asked but this might help someone someday. If there is an hour added to the date (3/5/2014 8:00:00 AM) and you would like to compare with your date you can:
'X being the date you want to compare
x = CDate(x) 'formatting the date using the CDate function
x= Format(x, "MM/DD/YYYY") 'formatting the date by dropping the hour
x= CDate(x) 'formatting the date again
If x <= Date Then
...
I follow your answer and No ! No ! And No!
On Excel VBA, It's wrong ! you need to format the date like "2020/07/18 09:48:51" to can compare
So first get Date variable with CDate() function, and after, you can compare like that. Else it's not working.
If Format(MyFileDate, "yyyymmdd hhnnss") >= Format(ReportStart, "yyyymmdd hhnnss") And Format(MyFileDate, "yyyymmdd hhnnss") < Format(ReportEnd, "yyyymmdd hhnnss") Then