Results 1 to 10 of 10

Thread: inserting a row(empty) afther each 4 rows of my data

  1. #1
    Junior Member saied's Avatar
    Join Date
    Jan 2015
    Posts
    5
    Rep Power
    0

    inserting a row(empty) afther each 4 rows of my data

    hi i want a code that do like this record macro and will continue up to end
    please send me acode

    Code:
    '
    ' Macro10 Macro
    '
    ' Keyboard Shortcut: Ctrl+b
    '
        Rows("5:5").Select
        Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
        Rows("10:10").Select
        Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
        Rows("15:15").Select
        Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
        Rows("20:20").Select
    2.jpg1.jpg
        End Sub
    Last edited by saied; 02-05-2015 at 12:10 PM.

  2. #2
    Junior Member
    Join Date
    Dec 2012
    Posts
    12
    Rep Power
    0
    Suppose there are some data in column A ...
    Try this code
    Code:
    Sub InsertEmptyRow()
        Dim LR As Long, I As Long, X As Long
        LR = Range("A" & Rows.Count).End(xlUp).Row
        X = Int(LR / 4) + LR
        For I = 5 To X Step 5
            Cells(I, 1).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
        Next I
    End Sub

  3. #3
    Forum Guru Rick Rothstein's Avatar
    Join Date
    Feb 2012
    Posts
    659
    Rep Power
    13
    Same idea as KingTamo's code, but a little more compact...
    Code:
    Sub Test()
      Dim R As Long
      For R = 5 To Cells(Rows.Count, "A").End(xlUp).Row Step 5
        Rows(R).Insert
      Next
    End Sub

  4. #4
    Junior Member
    Join Date
    Dec 2012
    Posts
    12
    Rep Power
    0
    Mr. Rick
    your code is great but it will not work after while ..
    Try to put any numeric values in range("A1:A102") for example .. and test your code
    you'll find that the last empty row is row 100 only !!

  5. #5
    Forum Guru Rick Rothstein's Avatar
    Join Date
    Feb 2012
    Posts
    659
    Rep Power
    13
    Quote Originally Posted by KingTamo View Post
    Mr. Rick
    your code is great but it will not work after while ..
    Try to put any numeric values in range("A1:A102") for example .. and test your code
    you'll find that the last empty row is row 100 only !!
    Good catch! Thanks for catching that... I had forgotten to account for the fact that the last row changes as the rows are inserted which is easily accounted for...

    Code:
    Sub Test()
      Dim R As Long
      For R = 5 To 5 * Cells(Rows.Count, "A").End(xlUp).Row Step 5
        Rows(R).Insert
      Next
    End Sub
    If the OP does not want to watch the screen jumping around, screen updating can be turned off, although then there is no visual feedback as to the macro's progress which may be important if there are a lot of rows to process (I have a fairly fast computer and the wait was quite noticeable for 20,000 rows).

    Code:
    Sub Test()
      Dim R As Long
      Application.ScreenUpdating = False
      For R = 5 To 5 * Cells(Rows.Count, "A").End(xlUp).Row Step 5
        Rows(R).Insert
      Next
      Application.ScreenUpdating = True
    End Sub

  6. #6
    Junior Member
    Join Date
    Dec 2012
    Posts
    12
    Rep Power
    0
    Mr Rick Rothstein
    thanks a lot for this great code
    I tried your code on my pc as following :
    I tried now 1000 rows ... calcuating the time elapsed for my code and yours.
    As for my code it takes 1.7 seconds but your code takes 6.6 seconds !!
    Here's the codes
    Code:
    Sub InsertEmptyRowKingTamo()
        Dim LR As Long, I As Long, X As Long
        LR = Range("A" & Rows.Count).End(xlUp).Row
        X = Int(LR / 4) + LR
        For I = 5 To X Step 5
            Cells(I, 1).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
        Next I
    End Sub
    
    Sub InsertEmptyRowRickRothstein()
        Dim R As Long
        For R = 5 To 5 * Cells(Rows.Count, "A").End(xlUp).Row Step 5
            Rows(R).Insert
        Next
    End Sub
    
    Sub CodeExecutionTime()
        Dim xStartTime As Double
        Dim xElapsedTime As Double
        xStartTime = Timer()
      
        Call InsertEmptyRowRickRothstein
        
        xElapsedTime = Timer() - xStartTime
            If xElapsedTime < 0# Then
                xElapsedTime = xElapsedTime + 86400#
            End If
        MsgBox "Elasped Time : " & xElapsedTime
    End Sub

  7. #7
    Forum Guru Rick Rothstein's Avatar
    Join Date
    Feb 2012
    Posts
    659
    Rep Power
    13
    Quote Originally Posted by KingTamo View Post
    Mr Rick Rothstein
    thanks a lot for this great code
    I tried your code on my pc as following :
    I tried now 1000 rows ... calcuating the time elapsed for my code and yours.
    As for my code it takes 1.7 seconds but your code takes 6.6 seconds !!
    Your code does not do what my code does which is why it takes less time... my code inserts entire rows (which is what I read the OP's request to be) whereas your code only inserts blank cells in Column A. Try adding data in Columns A and B and then run your code to see what I mean.

  8. #8
    Junior Member
    Join Date
    Dec 2012
    Posts
    12
    Rep Power
    0
    Mr. Rick
    I tried this modification and it takes the same period nearby
    Code:
    Sub InsertEmptyRowKingTamo()
        Dim LR As Long, I As Long, X As Long
        LR = Range("A" & Rows.Count).End(xlUp).Row
        X = Int(LR / 4) + LR
        For I = 5 To X Step 5
            Rows(I).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
        Next I
    End Sub
    While your code still takes the same long period ..

  9. #9
    Forum Guru Rick Rothstein's Avatar
    Join Date
    Feb 2012
    Posts
    659
    Rep Power
    13
    Quote Originally Posted by KingTamo View Post
    Mr. Rick
    I tried this modification and it takes the same period nearby
    Code:
    Sub InsertEmptyRowKingTamo()
        Dim LR As Long, I As Long, X As Long
        LR = Range("A" & Rows.Count).End(xlUp).Row
        X = Int(LR / 4) + LR
        For I = 5 To X Step 5
            Rows(I).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
        Next I
    End Sub
    While your code still takes the same long period ..
    That is because the modification I did in Message #5 over-compensated for what was actually needed. This should come in at the same timing as your code...

    Code:
    Sub Test()
      Dim R As Long
      For R = 5 To 1.25 * Cells(Rows.Count, "A").End(xlUp).Row Step 5
        Rows(R).Insert
      Next
    End Sub
    And this should be somewhat faster (you can do the same for your code to speed it up as well)...

    Code:
    Sub Test()
      Dim R As Long
      Application.ScreenUpdating = False
      For R = 5 To 1.25 * Cells(Rows.Count, "A").End(xlUp).Row Step 5
        Rows(R).Insert
      Next
      Application.ScreenUpdating = True
    End Sub

  10. #10
    Junior Member
    Join Date
    Dec 2012
    Posts
    12
    Rep Power
    0
    Now it is perfect Mr. Rick
    Thanks a lot for your patience and for your great code

Similar Threads

  1. Replies: 2
    Last Post: 03-08-2014, 02:49 AM
  2. Skip empty row and fetch values from other rows
    By dhivya.enjoy in forum Excel Help
    Replies: 1
    Last Post: 11-08-2013, 07:44 PM
  3. Sending Data From User Form To First Empty Row Of Sheets
    By paul_pearson in forum Excel Help
    Replies: 21
    Last Post: 08-14-2013, 11:04 PM
  4. Replies: 5
    Last Post: 07-11-2013, 07:31 AM
  5. Delete Empty Rows
    By Rasm in forum Excel Help
    Replies: 4
    Last Post: 04-28-2011, 02:13 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •