Current date plus time VBA - vba

I want to create a button which will do the following.
- Retrieve the current date and time -->
- This date and time plus the time in (O2) -->
- In the range of cells (L2:L11) needs to be displayed if the current time plus the time in cell O2 is less or more than the date and time in the cell next to it (Range M2:M11). The cell L2 will be compared with M2, L3 with M3 etc.
On any current time, you will be able to click on the button and it will calculate if it is on time or too late.
I know how to get the current date but can't get any further.
Click here for the image with the cells
Sub OnTime()
With Range("L2:L11")
.Value = Now()
.NumberFormat = "mm/dd/yyyy h:mm:ss AM/PM"
End With
End Sub

Why don't you simply do this
In cell O2 type "=now"
in cell L2 type "=IF(B1<D1,"Ontime", "Late")"
After that each time you refresh the formulaes it will be recalculated. Do that by linking this macro to a button
Sub Refresh_formulaes()
Calculate
End Sub

Related

Excel_VBA highlight cells which has current date and different time

Need help to highlight the cell which contains today's date and a different time. The code that I wrote is here, but it can only highlight the cells with the current date.
Sub Datechoose()
Dim rCell As Range
With Worksheets("owssvr")
For Each rCell In .Range("A2:M20")
If rCell.Value = Date Then
rCell.Interior.Color = vbGreen
End If
Next
End With
End Sub
enter image description here
My aim is to highlight the cells with the current date(6/22/2018 and time).
Please help me out...
Highlight your cells and give them a conditional format.
With A1 selected the condition below will return TRUE if the date portion of the date/time value is equal to todays date.
=INT(A1)=TODAY()
As #HarassedDad said - 12 noon will be 43273.5 for 22nd June 2018.
INT(43273.5) cuts off the time leaving just the date to compare against your date value.
Wrap the 2 comparison values in a format to ensure Excel reads them in the same manner.
if isdate(rCell) then
If format(rCell.Value, "mm/dd/yyyy") = format(Date, "mm/dd/yyyy") Then
rCell.Interior.Color = vbGreen
End If
end if
Edit 1
you'll get an overflow error if the cell value is a numeric value outside of valid date parameters. You can avoid this by using the 'isDate' function to verify the cell is a date and can handle the format.

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 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!

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

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

excel auto fill date based on current date

I need to auto fill a specific column with a date. If the date occurs between Saturday through Tuesday, I need to auto fill the date column with the previous Friday. If the date occurs between Wednesday and Friday I need to put in the coming Friday date for the auto fill.
For example: If I run the spreadsheet on Tuesday 10/23 I need the auto fill date to be Friday 10/19. If I run the spreadsheet on Wednesday 10/24 I need the auto fill date to be Friday 10/26.
Here is the formula I have so far, so I need to invoke this formula via macro when saving the spreadsheet or by clicking a custom button. Any assistance would be greatly appreciated.
=IF(ISBLANK($A2),"",IF(WEEKDAY(TODAY())<4,(TODAY()+(-1-WEEKDAY(TODAY()))),(TODAY()+(6-WEEKDAY(TODAY())))))
I'd suggest using VBA for what you want, something like this which would go in the ThisWorkbook module and run prior to the workbook saving:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
'Check if A2 is blank
If Cells(2, 1) = "" Then Exit Sub
'Find the date and use it
Dim x As Long
Dim dateToUse As Date
x = Weekday(Date, vbSaturday)
If x <= 4 Then
dateToUse = Date - x
Else
dateToUse = Date + (7 - x)
End If
'Change Cells(1, 1) to the actual target you want to have the date,
'which could be determined based upon the contents of your workbook/sheet.
Cells(1, 1) = dateToUse
End Sub