Consulting

Results 1 to 16 of 16

Thread: Insert alternative rows

  1. #1

    Insert alternative rows

    Need a macro to insert alternative rows in a selected range.Someone please provide

  2. #2
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    [vba]Sub Insert_rows()
    Dim Rng1 As Integer
    Dim i As Integer
    Rng1 = ActiveSheet.Range("A65536").End(xlUp).Rows
    For i = 2 To Rng1 Step 2
    ActiveSheet.Rows(i).Insert Shift:=xlDown
    Next i
    End Sub[/vba]
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  3. #3
    The code is inserting rows in the range containing data. But the need is that the code should insert rows in the selected range. Suppose rows 1 to 100 contain data & if rows 25 to 38 is selected than the code should start inserting rows alternatively after every row starting from 26,28,& so on till 38 row is reached

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    problem wit that Simon is that rows at the end don't get processed. The reason is that if you start with 10 rows, you loop goes 2 to 10 step 2, but after the first 3 iterations your range is 13 rows long, so you miss thos last 3 rows, and any further rows shifted.

    You need to go bottom up

    [vba]

    Sub Insert_rows()
    Dim Rng1 As Integer
    Dim i As Integer
    Rng1 = ActiveSheet.Range("A" & ActiveSheet.Rows.Count).End(xlUp).Rows
    For i = Rng1 To 2 Step -1
    ActiveSheet.Rows(i).Insert Shift:=xlDown
    Next i
    End Sub
    [/vba]

  5. #5
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    Yep!, i remember now Bob you keep telling me but i still keep constucting code in the top to bottom logic, i will try to remember in future.

    Try something like:

    [VBA]Dim i As Integer
    Dim sc As Integer
    sc = Selection.Rows.Count
    For i = sc To 0 Step -1
    Rows(i).Insert Shift:=xlDown
    Next i
    [/VBA]not tested but shoud do the trick!
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  6. #6
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    Just had a look at that, no it wont work!....back to the drawing board!
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  7. #7
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    I've been to the drawing board and this is what i came up with!
    [vba]
    Sub Macro1()
    Dim MyCell As Range
    Dim i As Integer
    Dim sa As String
    Dim sc As Integer
    sc = Selection.Rows.Count
    sa = Selection.Address
    i = 1
    For Each MyCell In Range(sa)
    MyCell.Offset(i, 0).Insert Shift:=xlDown
    i = i + 1
    If i >= sc Then Exit Sub
    Next
    End Sub
    [/vba]
    Last edited by Simon Lloyd; 05-19-2007 at 02:47 AM.
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  8. #8
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Why switch from finding the range as you did originally, to rqeuiring the range to be selected.

    And, isn't

    [vba]

    sa = Selection.Address
    For Each MyCell In Range(sa)
    [/vba]

    the same as

    [vba]
    For Each MyCell In Selection
    [/vba]

  9. #9
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    Apologies Bob just pure ineptitude!
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  10. #10
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    You were on the right track to start with, all you needed to do was go bottom to top rather than top to bottom. Everything else was good.

  11. #11
    Tha last code provided by Simon is inserting only one row irrespective of rows selection

  12. #12
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    Worked fine for me with a contiguous selection
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  13. #13
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    With prompting and proding from both Bob and Malcom!
    [VBA]
    Sub Macro1()
    Dim i As Integer
    For i = Selection.Rows.Count To 0 Step -1
    Selection.Rows(i).EntireRow.Insert
    Next
    End Sub

    [/VBA]
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  14. #14
    But i want to insert one by one alternate rows to the entire selection and not just only one.

  15. #15
    yeah the last code is working. thanks to all

  16. #16
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    A couple of points.
    If you don't want the first line to move then use
    [vba] For i = Selection.Rows.Count To 2 Step -1
    [/vba] If you don't want adjoining columns affected use
    [vba] Selection.Rows(i).Insert
    [/vba]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Posting Permissions

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