In excel, I have reports that give me a column of dates (m/d/yy in Column A) and a column of times (hh:mm:ss in Column B). I have an issue with the dates being listed as the previous day if the time is before "07:00:00". To fix this issue, I normally run an IF statement in the "C" column that says =IF(B2<$C$1,A2+1,A2). In cell "C1" I manually put in the "07:00:00" and start the IF statement at C2 and copy it down to the rest of the entries. I also note that when I get this report in excel, the time column is set to the format of "General".
I am running into an issue trying to make this run in a macro so I do not have to take the extra time to do this (and some other) formatting. I have gotten the IF statement into the cells I want but none of the dates that are incorrect are changing. This seems to be because of the formatting of the times (and possibly dates?) . I attempted to use the .NumberFormat = "hh:mm:ss" to make sure that all of the referenced and new cells were the same but there is still no change. Am I missing something to make the formats match up? Here is a sample of the code I am using:
This question is also posted at: macros - Set date/ time formats in a VBA to allow IF statement to work - Stack Overflow
I am running into an issue trying to make this run in a macro so I do not have to take the extra time to do this (and some other) formatting. I have gotten the IF statement into the cells I want but none of the dates that are incorrect are changing. This seems to be because of the formatting of the times (and possibly dates?) . I attempted to use the .NumberFormat = "hh:mm:ss" to make sure that all of the referenced and new cells were the same but there is still no change. Am I missing something to make the formats match up? Here is a sample of the code I am using:
Code:
Sub Date_Change()
Dim w1 As Worksheet
Set w1 = Worksheets("Sheet1")
w1.Range("C1").FormulaR1C1 = "07:00:00"
w1.Range("C1").NumberFormat = "hh:mm:ss"
w1.Range("A:A").NumberFormat = "m/d/yy"
w1.Range("B:B").NumberFormat = "hh:mm:ss"
w1.Range("C2").Formula = "=IF(B2<$C$1,A2+1,A2)"
w1.Range("C2").NumberFormat = "m/d/yy"
w1.Range("C2").Select
ActiveCell.Copy
ActiveCell.AutoFill Destination:=Range(ActiveCell.Offset(0, -1),
ActiveCell.Offset(0,-1).End(xlDown).Offset(-1, 0)).Offset(0, 1)
End Sub
This question is also posted at: macros - Set date/ time formats in a VBA to allow IF statement to work - Stack Overflow