How could I use vba to check if dates are the same? - vba

I'm trying to write a code on cells that have both dates and times in them (Example: 1/29/14 9:15 AM). I'm trying to see if the date from 1 cell matches the date on another cell. How could I do that?
Example:
A1: 2/13/14 B1: 2/13/14 - This would return True
A2: 1/15/14 B2: 4/25/14 - This would return false
Any way I could achieve this?
Thanks

Excel stores dates as fractional numbers where the whole number represents the date and the fraction represents the time of day (e.g., 0.5 = noon). The whole number is the number of days since 1/1/1900. So Excel stores 1/3/1900 6:00 PM internally as 3.75.
As long as they are formatted as dates, you can compare them directly:
C1 formula: =(A1=B1)
C2 formula: =(A2=B2)
To ignore the time portion of the date, take the integer portion only:
C1 formula: =(INT(A1)=INT(B1))
C2 formula: =(INT(A2)=INT(B2))
There's no real need for VBA, but if you want to compare the dates in VBA, the same rules apply:
Sub CompareDates()
With ActiveSheet
.[C1] = (Int(.[A1]) = Int(.[B1]))
.[C2] = (Int(.[A2]) = Int(.[B2]))
End With
End Sub

Related

Formula to convert a week number to month

I am trying to implement VBA code for converting the week number to month Name.
I have a sheet in which column "A" contains week number from 1 to 52 . The week number 1 starts from column A3. I want the corresponding month Name for the week number to be printed.
I tried a formula like this
=MONTH(DATE(YEAR(NOW()),1,1)+(B1*7))
The reason I tried an formula fisrt is to get the idea and then converting the formula to VBA.
I am just getting the formula printed in my cell. Can anyone help me with this?
here you go:
dim i as long
dim n as long
n=sheets("Sheet1").cells(Rows.count,1).end(xlup).row
for i = 1 to n
cells(i,3).formula = "=MONTH(DATE(YEAR(NOW()),1,1)+(RC[-1]*7))"
next i

Rounding Date and Time in VBA

How can I round off date and time in excel using VBA?
For example, the user selects the value from the calendar which is copied in Cell A6 = "08/25/2016 09:02:00"
I am pulling the data in 15 minutes interval so I want it to be A6 = "08/25/2016 09:00:00"
So if the user selects any date and time that is not in multiple of 15 minutes, it should go back to the previous 15 minute interval value and pull the data.
Pull out the minutes, floor the date portion to get rid of the time, then add it back by building it with TimeSerial:
Private Sub Example()
Dim foo As Date
foo = CDate("08/25/2016 09:02:00")
Dim minutes As Long
minutes = Minute(foo)
minutes = 15 * (minutes \ 15) 'Round down by 15 minute increments
foo = Int(foo) + TimeSerial(Hour(foo), minutes, 0)
Debug.Print foo
End Sub
Edit: Like #Pekka mentions, this can be done with a worksheet formula too - this is the equivalent to the code VBA above:
=INT(A6)+TIME(HOUR(A6),INT(MINUTE(A6) / 15) * 15, 0)
VBA is not necessary. This can be done directly in Excel. =FLOOR(A6,TIME(0,15,0)) will truncate a date time value to the previous 15 minute value.
Excel represents date values as a floating point value since an initial date (around 1900, depending on version) with the time as the fractional portion of the value.
You could, of course, use the same expression in VBA code in the same way.
As Jeeped comments, this is a more self-documenting alternative to the more direct expression =int(A6*24*4)/4/24 initially suggested.
A bit shorter version of the other answers
=MRound(A6, 1/96)
which in VBA can be
[a6] = [MRound(A6, 1/96)]
or to round down
[a6] = [Int(A6*96)/96]

Calculate time difference in VBA for Word

I would like to calculate the time difference between 2 cells and the result showing in the 3rd cell of a MS WORD TABLE.
For instance, cell D2 showing 2pm and D1 showing 1pm and the difference in cell B2
Sub DetermineDuration()
Dim dtDuration As Date
dtDuration = DateDiff("h:mm:ss", D2, D1)
Range("B2").dtDuration
End Sub
Dates can be subtracted like numbers:
Sub DetermineDuration()
Range("B2") = Range("D2") - Range("D1")
End Sub
Make sure to format cell B2 as Time our you will just end up with a floating point number.
You can use the DateDiff function to return a specific interval type, like hours, minutes or seconds, without precision. It returns a regular number, not a time value. In the interval parameter you have to specify a valid interval value. You can learn about the function and see the valid interval values here MS Excel: How to use the DATEDIFF Function (VBA)
yyyy Year
q Quarter
m Month
y Day of year
d Day
w Weekday
ww Week
h Hour
n Minute
s Second
As you can see, "h:mm:ss" is not a valid interval. You can use this format to get the seconds difference:
DateDiff("s", Range("D2"), Range("D1"))
If you want to get a time value, you can convert using the TimeSerial function.

Excel Formula or VBA to convert year, month data to actual date

I have 2 columns (year and month). A sample data of year column A is 2015 at cell A2. A sample data of month column B is Jun at cell B2. I would like to convert these data to date format in column C to '2015-06-00` at cell C2. Is there a formula or Excel VBA to do this?
First off, 2015-06-00 is actually 31-May-2015. If you supply zero as the day-of-the-month, you get the last day of the previous month. Typically you would set it for the first day of the month. Use the DATE function for this with 1 as the day_num parameter or the DATEVALUE function if you are stringing together text-that-looks-like-a-date.
=datevalue(B2 & " 1, " & A2)
With a three letter month in B2, you will have to translate that to a numerical month if you opt for the DATE function.
=DATE(A2, LOOKUP(B2, {"Apr","Aug","Dec","Feb","Jan","Jul","Jun","Mar","May","Nov","Oct","Sep"}, {4,8,12,2,1,7,6,3,5,11,10,9}), 1)
in cell C "=FECHA(A2;B2;1)" (spanish excell)

Excel VBA code to compare system date with date in the worksheet

I want a vba code to compare the system date with the date i mentioned in a row (A) in excel worksheet. If the dates are equal then it should compare the system time and the time mentioned in a row (B). Eg: If the time mentioned in the sheet is 7 30 PM and the system time is 7 PM, it should calculate the time between and display. If the date in the sheet does not matches then it should leave.
Thanks in advance.
I put together a small procedure to get you started. It assumes the date/time in the worksheet is in cell A1. If the system date is the same as the date in cell A1, it then compares the system time with the time in cell A1. If they are the same to the minute then nothing happens.
However, if they are different then the number of minutes by which they are different appears in cell B1.
Here is the code:
Public Sub DateTimeCheck()
Dim d As Double
Dim s1 As String
Dim s2 As String
s1 = Format([a1], "dd-mm-yyyy hh:mm")
s2 = Format(Now, "dd-mm-yyyy hh:mm")
If Left$(s2, 10) = Left$(s1, 10) Then
d = TimeValue(Right$(s2, 5)) - TimeValue(Right$(s1, 5))
If d Then
[b1] = d * 1440
End If
End If
End Sub
Now, if you have more cells with dates in column A and you want to compare them as well, then you will need to modify this code so that it "processes" all of them.
As far as writing the difference in minutes to the email... we would need much more detail for that!