Macro to insert rows

bradyj7

Board Regular
Joined
Mar 2, 2011
Messages
106
Hi there,

In column A I have time values as in the example below. You'll see that they are every 2-5 secs approximately. The number of rows varies considerably in each workbook depending on how long the recording lasted. I'm looking for a macro that will find the last row of data in Col A and insert rows between the ones that are there so that there is a row for every second.

Code:
 Date 
28/03/2011 08:08:23
28/03/2011 08:08:26
28/03/2011 08:08:31
28/03/2011 08:08:37
28/03/2011 08:08:41
28/03/2011 08:08:47
28/03/2011 08:08:51

So as an example I'd like it to do this
Code:
28/03/2011 08:08:23
28/03/2011 08:08:24
28/03/2011 08:08:25
28/03/2011 08:08:26
etc

Appreciate any ideas

Thanks

John
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Try this

Code:
Sub GoodTimes()
Dim i As Long, j As Long
Dim FR As Long: FR = 1
Dim LR As Long: LR = ActiveSheet.Range("A" & ActiveSheet.Rows.Count).End(xlUp).Row
Dim Str As String
Dim Tm1 As Date
Dim Tm2 As Date
Dim Tm3 As Integer
Dim Dif As Double
Dif = 1 / 24 / 60 / 60
For i = LR To (FR + 1) Step -1
      Tm1 = Cells(i - 1, 1)
      Tm2 = Cells(i, 1)
      Tm3 = DateDiff("s", Tm1, Tm2)
      
      If Tm3 > 1 Then
            For j = (Tm3 - 1) To 1 Step -1
                  Rows(i).Insert Shift:=xlDown
                  Cells(i, 1) = Tm1 + (Dif * j)
                  Cells(i, 1).NumberFormat = "dd/mm/yyyy hh:mm:ss"
            Next j
      End If
Next i
End Sub
 
Upvote 0
Hi,

Thanks for the macro above. I call it in inside another macro.

It works in as so far that it inserts the rows in the appropriate areas but when it gets to the end of column its crashes with a "type mismatch" error on this line and doesn't continue on with the rest of the bigger macro.
Code:
 Tm1 = Cells(i - 1, 1)

Would you know why this is happening?

Thank you
 
Upvote 0
Sorry, its obvious why - The first row has a heading in it, Just had to change Fr=2

Thanks
 
Upvote 0
I copied what you posted

Code:
 Date 
28/03/2011 08:08:23
28/03/2011 08:08:26
28/03/2011 08:08:31
28/03/2011 08:08:37
28/03/2011 08:08:41
28/03/2011 08:08:47
28/03/2011 08:08:51


</PRE>

and pasted into my worksheet. The code I posted worked on it fine but I assumed you had already formatted the entire column as date. What you posted was all in one column right? It's not column A: 28/03/2011 and column B: 08:08:23 is it?
 
Upvote 0
Code:
Sub GoodTimes()
Dim i As Long, j As Long
Dim FR As Long: FR = 1
Dim LR As Long: LR = ActiveSheet.Range("A" & ActiveSheet.Rows.Count).End(xlUp).Row
Dim Str As String
Dim Tm1 As Date
Dim Tm2 As Date
Dim Tm3 As Integer
Dim Dif As Double
Dim Val1 As Single
Dim Val2 As Single
Dim Val3 As Single
Dif = 1 / 24 / 60 / 60
For i = LR To (FR + 1) Step -1
      Tm1 = Cells(i - 1, 1)
      Tm2 = Cells(i, 1)
      Tm3 = DateDiff("s", Tm1, Tm2)
      Val1 = CSng(Cells(i - 1, 2))
      Val2 = CSng(Cells(i, 2))
      Val3 = (Val2 - Val1) / Tm3
      
      If Tm3 > 1 Then
            For j = (Tm3 - 1) To 1 Step -1
                  Rows(i).Insert Shift:=xlDown
                  Cells(i, 1) = Tm1 + (Dif * j)
                  Cells(i, 1).NumberFormat = "dd/mm/yyyy h:mm:ss"
                  Cells(i, 2) = Val1 + j * Val3
            Next j
      End If
Next i
End Sub

Let me know if it works.
 
Upvote 0

Forum statistics

Threads
1,224,506
Messages
6,179,159
Members
452,892
Latest member
yadavagiri

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