I have two columns of 15 minute date/time interval data. One is formatted in 24-hour time and the other 12-hour. These come from two different sources, and reformatting the source data is not an option. All of these intervals are showing as exact matches (even as values), but when I use a lookup formula (INDEX/MATCH & VLOOKUP) or even VBA, a match for every third interval is not being identified. I have attached sample pictures with formulas as well as my code below. Thanks in advance!
Sub MatchTest()
Dim i As Integer
For i = 2 To 22
If CDate(Cells(i, 1).Value) = CDate(Cells(i, 3).Value) Then
Cells(i, 8) = Cells(i, 2)
ActiveCell.Offset(1, 0).Select
Else:
Cells(i, 8) = "NA"
ActiveCell.Offset(1, 0).Select
End If
Next i
End Sub
Instead of Cdate, Timevalue and Datevalue can be used can be used. The TIMEVALUE function returns a serial number of a time. The DATEVALUE function converts a date that is stored as text to a serial number that Excel recognizes as a date.
For instance, Timevalue of 08/12/2008 20:00:00 and 08/12/2008 08:00PM yields the same result 0.833333333335759
Datevalue of same date in different format will also yield same result
TIMEVALUE(TEXT(yourdate, "DD/MM/YYYY HH:MM:SS"))
TIMEVALUE(TEXT(yourdate, "DD/MM/YYYY HH:MM AM/PM"))
DATEVALUE(TEXT(yourdate, "DD MMMM YYYY"))
Hey all thanks for your answers. All three of these methods worked.
If TimeValue(Cells(i, 1).Value) = TimeValue(Cells(i, 3).Value) Then
If DateValue(Cells(i, 1).Value) = DateValue(Cells(i, 3).Value) Then
If Abs(CDate(Cells(i, 1).Value) - CDate(Cells(i, 3).Value)) < 0.000001 Then
Related
I am attempting to remove all dates before a certain time period, as well as anything that is not a date from my data set. I have roughly 4000 entries in column A dating back the last 10 years, with some typo's mixed in. There are no blanks spots between the data.
I have cobbled together the below code, which almost works. However there is one entry 31/12/1019 which is not being picked up as an old date, or a typo.
Sub deleterows()
lastRow = Sheets("ConData").Cells(Rows.Count, 1).End(xlUp).Row
bankingDate = DateSerial(Year(Date), Month(Date), 0)
For i = lastRow To 1 Step -1
If IsDate(Cells(i, 1)) = False Or _
Cells(i, 1).Value <= bankingDate Then Rows(i).EntireRow.Delete
Next
End Sub
Any help would be appreciated.
Let's break it down, this is a wonderful inconsistency between Excel and VBA:
According to THIS article:
In Windows, the range of valid dates is January 1, 100 A.D., through December 31, 9999 A.D.; the ranges vary among operating systems.
so IsDate will return TRUE for 31/12/1019
But
Since Excel actually stores "dates" as a double with 1900-01-01 being 1.00 the date would be stored as a string in the worksheet and the Cells(i, 1).Value <= bankingDate would return False because a string is larger than a number.
But as #BigBen stated:
Cast to a date first before comparing to bankingDate.
If Not IsDate(Cells(i, 1).Value) Then
Rows(i).EntireRow.Delete
ElseIf CDate(Cells(i, 1).Value) <= bankingDate
Rows(i).EntireRow.Delete
End If
IsDate will give true but you can check then for
isNumeric(Cells(i, 1).value2
If not IsDate(Cells(i, 1)) or not isNumeric(Cells(i, 1).value2) or ...
Value2 gives a double for Date Cells which are later then 0.1.1900
And I believe your banking date will always be later 😀
I have a program that is supposed to read the date from a cell. In that cell, I have given it the value of =NOW() just by typing it into the cell outside of VBA. The cell is formatted as a date and the format is: dd-month (for example; 28-Jan). When VBA reads the cell, it reads it as mm/dd/yyy 00:00:00 AM/PM. Is there a way to make my code read the month from the format I set? A section of my code is below:
dashpos = InStr(1, ThisWorkbook.Worksheets("Main").Cells(2, 15), "-")
curmonth = Right(ThisWorkbook.Worksheets("Main").Cells(2, 15).Value, dashpos + 1)
The cell containing the date is Cell(2,15). I then go on to use the three letters on the month to determine the following month using a Select Case curmonth.
If your format is mm/dd/yyy 00:00:00 AM/PM in the worksheet, then the month will always have two digits. Therefor:
curmonth = CLng(Left(ThisWorkbook.Worksheets("Main").Cells(2, 15).Text, 2))
Sub testDateExtraction()
'Next day in the format you use in the sheet (no matter, in fact...):
Debug.Print Format(ThisWorkbook.Worksheets("Main").Cells(2, 15).Value + 1, "dd-mmm")
'Next month
Debug.Print MonthName(Month(ThisWorkbook.Worksheets("Main").Cells(2, 15).Value) + 1, True)
'If you insists to use the string type data:
Dim strDate As String, strMonth As String
strDate = CStr(Format(ThisWorkbook.Worksheets("Main").Cells(2, 15).Value + 1, "dd-mmm"))
strMonth = Right(strDate, 3)
Debug.Print MonthName(Month(DateValue(Day(Date) & "-" & strMonth & "-2020")) + 1, True)
End Sub
I then go on to use the three letters on the month to determine the following month
If you want to determine the next month, you can just use DateAdd and Month. The cell format is irrelevant.
The following returns the month number:
Month(DateAdd("m", 1, Cells(2,15)))
If you want it as a three letter string, for some reason, then
Format(DateAdd("m", 1, Cells(2,15)), "mmm")
A text box in a user form has the vba below in order to paste the date in column D on my worksheet. I need the format of the date to be pasted as follows: "YYYY/MM/DD" exactly in this order.
However at the moment the code below pastes the date "DD/MM/YYYY". Am i missing something? thanks
.Cells(lRow, 4).Value = Me.txtDate.Value
.Cells(lRow, 4).NumberFormat = "YYYY/MM/DD"
A textbox contains a String. A date-formatted cell contains a Date (if you want Excel to understand it as a date anyway). You're missing a conversion.
Dim dateValue As Date
dateValue = ToDate(Me.txtDate.Value)
.Cells(lRow, 4).Value = dateValue
.Cells(lRow, 4).NumberFormat = "yyyy/MM/dd"
Where ToDate would be a function that takes a String and returns a Date. There are many ways to go about this, and you'll want to handle the situation where the provided string isn't a valid date.
Here's one way:
Private Function ToDate(ByVal value As String) As Date
ToDate = DateValue(value) 'raises error 13 "type mismatch" if invalid
End Function
If that's good enough, and only needed in one place, then you could inline it:
.Cells(lRow, 4).Value = DateValue(Me.txtDate.Value)
.Cells(lRow, 4).NumberFormat = "yyyy/MM/dd"
my user form has three separate combo boxes for the users to select the following: Year, Month, Date (they are not dependent). For example if the user selects 2017 (the year), May(the month) and 23(the day) I want vba to enter in column two on my database sheet: "2017/05/23". I want it exactly in this format as a value.
The code below doesn't work. Any help would be appreciated.
With ws
.Cells(lRow, 2).Value = Me.cboYear / Me.cboMonth / Me.cboDay.Value
End With
You're dividing the year by the month, and then dividing that by the day. That's the value your cell is getting.
Build an actual date instead.
.Cells(lRow, 2).Value = DateSerial(cboYear, cboMonth, cboDay)
And then format that date any way you wish, using NumberFormat:
.Cells(lRow, 2).NumberFormat = "yyyy/MM/dd"
DateSerial expects integer values. If your cboMonth contains strings (month names), then you need to work a bit harder for it - the easiest is probably just to use a keyed collection - specify a string key when .Adding the integers to the collection:
Static months As Collection
If months Is Nothing Then
Set months = New Collection
With months
.Add 1, "January"
.Add 2, "February"
.Add 3, "March"
'...
.Add 12, "December"
End With
End If
And then you have an easy lookup:
Dim monthId As Integer
monthId = months(cboMonth)
So your DateSerial would be:
.Cells(lRow, 2).Value = DateSerial(cboYear, monthId, cboDay)
I have a cell value, for example
A1 = 17/06/2016 19:00:46
i want to change it to 17/06/2016 19:00:00
so basically the seconds have to be 0 but i can not seem to be able to achieve that with the formatting. I did the format as dd/mm/yyyy hh:mm, but the seconds is still retaining.
i will be using these values to match the values in a different sheet using Application.Match in vba. the different sheet has the seconds as 0, hence, to match it i need to be convert these to 0 seconds.
Thank you.
The easier way to achieve this:
Function DateWithZeroSeconds(MyDate As Date)
DateWithZeroSeconds = Format(MyDate, "dd/mm/yyyy h:m") & ":00"
End Function
Select the cells you wish to convert and run this short macro:
Sub SecondKiller()
Dim d As Date, d2 As Date
For Each r In Selection
d = r.Value
r.Value = DateSerial(Year(d), Month(d), Day(d)) + TimeSerial(Hour(d), Minute(d), 0)
Next r
End Sub
An other way, format the Excel cell, and then show the seconds as 00's:
Sub FormatRange_AsDate(ByVal Rg As Range)
With Rg
.NumberFormat = "d/m/yyyy h:mm:ss;#" 'give the format to the cell , showing seconds
.Value = Format(.Value, "d/m/yyyy h:m") & ":00" 'changing the seconds to "00", don't use .value2
End With
End Sub