Set date/ time formats in a VBA to allow IF statement to work

cpotts13

New Member
Joined
Feb 5, 2013
Messages
23
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:

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
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top