Solved

Macro to insert "X" number of rows between items in a list

Posted on 2014-02-14
3
344 Views
Last Modified: 2014-02-14
I have a list of 100+ items and I would like insert 52 rows between each item in my list. I know this is probably pretty straightforward, but I don't have the time to figure it out and I know someone on here can help pretty quickly.

I've attached an example of my list.
InsertRowsExample.xlsx
0
Comment
Question by:thomas-sherrouse
3 Comments
 
LVL 19

Accepted Solution

by:
Ken Butters earned 500 total points
ID: 39860180
Here is code... need to run InsertLines Macro.

I have variable set to 52... you can change that if you want different number of lines inserted.

Public Sub InsertLines()

Dim myLastCell As Range
Dim i As Long
Dim ws As Worksheet

Dim numRowsToInsert As Long

numRowsToInsert = 52


Set ws = Worksheets("Sheet1")
Set myLastCell = LastCell(ws)

For i = myLastCell.Row To 2 Step -1
    ws.Rows(i & ":" & i + numRowsToInsert).Insert shift:=xlDown
Next

End Sub


Function LastCell(ws As Worksheet) As Range

'
' Note "&" denotes a long value; "%" denotes an integer value
  
    Dim LastRow&, lastCol%

' Error-handling is here in case there is not any
' data in the worksheet

    On Error Resume Next

    With ws

  ' Find the last real row

    LastRow& = .Cells.Find(What:="*", _
      SearchDirection:=xlPrevious, _
      SearchOrder:=xlByRows).Row

  ' Find the last real column

    lastCol% = .Cells.Find(What:="*", _
      SearchDirection:=xlPrevious, _
      SearchOrder:=xlByColumns).Column

    End With

' Finally, initialize a Range object variable for
' the last populated row.
    
    Set LastCell = ws.Cells(LastRow&, lastCol%)


End Function

Open in new window

InsertRowsExample.xlsm
0
 
LVL 33

Expert Comment

by:Norie
ID: 39860204
This will insert 52 rows between each row.
Dim rng As Range

    Set rng = Range("A2")

    Do

        rng.Offset(1).Resize(52).Insert xlShiftDown

        Set rng = rng.Offset(53)

    Loop Until rng.Value = ""

Open in new window


This will insert 52 rows whenever there's a change in value.
Dim rng As Range

    Set rng = Range("A2")

    Do

        If rng.Value <> rng.Offset(1).Value Then
            rng.Offset(1).Resize(52).Insert xlShiftDown
               Set rng = rng.Offset(52)
        End If
     
        Set rng = rng.Offset(1)

    Loop Until rng.Value = ""

Open in new window

0
 

Author Closing Comment

by:thomas-sherrouse
ID: 39860209
Thanks - Got what I needed!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now