Consulting

Results 1 to 4 of 4

Thread: Solved: Insert Row every nth row

  1. #1

    Solved: Insert Row every nth row

    I have a large amount of data (>5000 rows) that I need to copy and paste into word (as a picture). In order to do this, I need to break up the data into manageable tables (~32 rows) that will fit on a page. I've tried variations on the code below, but I get too many Title rows and I end up with too many tables in my document.

    [vba]Sub Insert_Row()
    Dim a As Byte
    Dim c As Integer
    Range("A1").Select
    a = 30
    c = 0

    While ActiveCell.Value <> ""

    c = c + 32
    Rows("1:1").Copy
    ActiveSheet.Rows(c).Insert Shift:=xlDown
    ActiveSheet.Rows(c).Insert Shift:=xlDown
    ActiveCell.Offset(a, 0).Select
    Wend
    End Sub
    [/vba]

    If anyone can alter this code or shove me in the right direction I'd appreciate it.

    Thanks!

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    Sub Insert_Row()
    Dim LastRow As Long
    Dim i As Long

    With ActiveSheet

    LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
    For i = LastRow - LastRow Mod 32 To 32 Step -32

    .Rows(i + 1).Insert
    Next i
    End With
    End Sub
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    The general rule for inserting or deleting rows is to start at the bottom, otherwise any Looping codes tend to go wrong.
    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'

  4. #4
    Thanks for the code and the advice!

Posting Permissions

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