macro adding rows


D

dobbers

hi,

I have a worksheet that contains data formatted with name start date and
time and finish date and time. I am looking for a macro to insert a row after
each date for example

21/09/2009 13:54 21/09/2009 14:05
21/09/2009 16:59 21/09/2009 18:11

22/09/2009 06:57 22/09/2009 07:15
22/09/2009 07:59 22/09/2009 08:15
22/09/2009 09:29 22/09/2009 09:35
22/09/2009 10:52 22/09/2009 11:14
22/09/2009 12:35 22/09/2009 13:02
22/09/2009 15:04 22/09/2009 15:15
22/09/2009 16:49 22/09/2009 17:18

23/09/2009 06:58 23/09/2009 07:14
23/09/2009 07:51 23/09/2009 08:20
23/09/2009 09:00 23/09/2009 09:18
23/09/2009 12:07 23/09/2009 12:13
23/09/2009 12:40 23/09/2009 12:52
23/09/2009 16:29 23/09/2009 16:50
23/09/2009 17:07 23/09/2009 17:25

As you can see there could be multiple start times for any day any help or
advice then please let me know as it is taking far to long to do manually

Thanks,

James
 
Ad

Advertisements

P

Per Jessen

Hi

Try this:

Sub InsertRow()
TargetCol = "A"
Firstrow = 2 'Headings in row 1
LastRow = Cells(Rows.Count, TargetCol).End(xlUp).Row

For r = LastRow - 1 To Firstrow Step -1
If Cells(r, TargetCol).Value <> Cells(r + 1, TargetCol) Then
Rows(r + 1).Insert
End If
Next
End Sub

Regards,
Per
 
P

Patrick Molloy

I'm assuming that the second set of dates will be the same as the first
(diiferent times obviously) and that the first set are in column A

the dateserial() function is used to strip off the time, so that the dates
can be compared directly

open the VBA editor (ALT+F11) then add a new code module (Insert/Module) and
paste in the following code:

Option Explicit
Sub Main()
Dim thisRow As Long
Dim dn As Date
Dim dn_1 As Date
For thisRow = Cells(Rows.Count, "A").End(xlUp).Row To 2 Step -1
dn = Cells(thisRow, 1)
dn = DateSerial(Year(dn), Month(dn), Day(dn))
dn_1 = Cells(thisRow - 1, 1)
dn_1 = DateSerial(Year(dn_1), Month(dn_1), Day(dn_1))
If dn <> dn_1 Then
Rows(thisRow).Insert
End If
Next
End Sub
 
D

dobbers

Hi,

Tried both of the options and bothe returned a mismatch error

Thanks,

James
 
P

Patrick Molloy

we both assumed your line was two dates with times in two separate columns.
I stated that was my presumption.
isn't that the case? What are the details of the mismatch?
 
D

dobbers

Hi,

The dates are in columns D and E i have other information before them such
as names vehicle registrations and a project code

Thanks,

james
 
Ad

Advertisements

P

Patrick Molloy

so with my code you changed the column to 4 and/or with Per's you changed the
target column to D ?
what was the mismatch relating to?
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top