Do you have a specific reason for wanting to do this in VBA?
I solved a similar problem a few months ago using formulas;
I have adapted that solution to your problem.
But first, let me make sure that I understand your problem.
As I understand it, you have a worksheet that looks like this:
A B C D E F
1 Date Start End User Color Overnight
2 5/28/2015 15:00 21:00 Fred Green
3 6/1/2015 16:25 1:00 Henry Red 1
4 6/4/2015 9:00 13:00 Mary Blue
where each row represents an event, with a date, start time, end time,
and other data. Some of the events occur overnight
(i.e., they begin before midnight and end after midnight).
You have marked those events with a 1 in Column F
(although there’s no need to do this manually;
you could simply set F2 to =B2>C2 and drag down).
No event lasts more than 24 hours, so there’s no possibility
of an event starting at 10:00 on Monday and ending at 11:00 on Tuesday
(or, if there is, you have some way of identifying those events
that you haven’t described, and you have set Column F appropriately).
In any case, no event spans more than two days
(e.g., you will never have an event beginning at 23:00 on Monday
and running until 1:00 on Wednesday).
You want to split each multi-day (i.e., overnight) event into two rows:
one on the first day, from the start time until midnight (or 23:59),
and another on the second day, from midnight until the end time.
You want all the other data associated with the event to be replicated
on both rows. So, for the above data, you want
A B C D E
1 Date Start End User Color
2 5/28/2015 15:00 21:00 Fred Green
3 6/1/2015 16:25 23:59 Henry Red
4 6/2/2015 0:00 1:00 Henry Red
5 6/4/2015 9:00 13:00 Mary Blue
as a result.
My Solution:
I will assume that you use no more than 23 columns,
so Columns X, Y, and Z are available for use as “helper columns”.
- Create a new sheet.
Assume that the existing sheet is
Sheet1 and the new sheet is Sheet2.
The following steps will copy the data from Sheet1 to Sheet2,
splitting rows.
- Copy the column headings from
Sheet1, row 1, to Sheet2, row 1.
- Enter
=INDEX(Sheet1!A:A, $X2)+$Y2 into Sheet2!A2.
- Enter
=IF($Y2=0, INDEX(Sheet1!B:B, $X2), 0) into Sheet2!B2.
- Enter
=IF(AND($Y2=0,$Z2>0), TIME(23,59,59), INDEX(Sheet1!C:C, $X2)) into Sheet2!C2.
- Enter
=INDEX(Sheet1!D:D, $X2) into Sheet2!D2
and drag/fill to the right to cover all your data
(i.e., to column E, in my example).
- Copy
Sheet1:A2:E2 and paste formats (and column widths, if desired)
onto Sheet2:A2:E2.
- Enter
2 in Sheet2!X2.
This designates the row on Sheet1 that this row (on Sheet2)
will pull data from.
- Enter
0 in Sheet2!Y2.
- Enter
=INDEX(Sheet1!F:F, $X2) into Sheet2!Z2.
This creates a local copy of the “overnight” indicator.
- Select
Sheet2!A2:Z2 and drag/fill down to row 3.
- Change
Sheet2!X3 to =IF(AND(Y2=0,Z2>0), X2, X2+1).
- Change
Sheet2!Y3 to =IF(AND(Y2=0,Z2>0), Y2+1, 0).
- Select
Sheet2!A3:Z3 and drag/fill down as far as you need
to get all your data.
It should look something like this:
A B C D E X Y Z
1 Date Start End User Color
2 5/28/2015 15:00 21:00 Fred Green 2 0 0
3 6/1/2015 16:25 23:59 Henry Red 3 0 1
4 6/2/2015 0:00 1:00 Henry Red 3 1 1
5 6/4/2015 9:00 13:00 Mary Blue 4 0 0
Notes:
- As stated in the instructions,
Sheet2!Xn specifies
the row on Sheet1 that row n (on Sheet2) will pull data from.
Sheet2!Yn is a one-up number
within a Sheet2!Xn value; i.e., within a Sheet1 row; i.e., within an event.
For an overnight event, Y will be 0 for the pre-midnight segment and 1 for the post-midnight segment.
For example, since rows 3 and 4 on Sheet2 pull data fromSheet1 row 3,
we have X3=X4=3, and Y3, Y4 = 0, 1.
To make this permanent, you can copy and paste values,
and delete columns X, Y, and Z.