Solved

insert copied rows in Excel table based on value in column

Posted on 2013-06-10
2
482 Views
Last Modified: 2013-06-10
Hi,

I've attached an example, but what I'm trying to do is insert (and copy if at all possible) rows to an Excel table based on a value in another column. my primary data is in columns A:K.  I've given an example of what I'd like the macro to produce in columns M:W.

Column K contains the number of rows to be inserted either directly before the row itself or directly after.  It shouldn't matter as long as it is consistent. If possible, I'd like the inserted rows to be copies of the row itself.

The best case scenario would be all of the above, plus column D being changed on the initial row and copies to the value of 1.00.  Basically I'm trying to expand a single row of 15 qty into 15 rows of 1 quantity.  Please let me know what macro would accomplish this.  

Thanks!
eric
addRowsExample.xlsx
0
Comment
Question by:emiller1680
2 Comments
 
LVL 6

Accepted Solution

by:
Michael earned 500 total points
ID: 39235741
Hi,

I've tested the following code on your sample data and I believe it does what you've requested.
Be sure to test it on a copy of your workbook.
Also, there can't be any tables in the columns after column K, otherwise it will generate an error.


Sub insertRows()
    
    Dim objLi As ListObject
    Dim i As Long
    Dim rng As Range
    Dim rw As Long
    Dim nRws As Long
    
    Application.ScreenUpdating = False
    
    Set objLi = ActiveSheet.ListObjects(1)
    With objLi
        For i = .ListRows.Count To 1 Step -1
            If .ListRows(i).Range(1, .ListColumns.Count) > 0 Then
                Set rng = .ListRows(i).Range
                Rows(rng.Row + 1 & ":" & rng.Row + .ListRows(i).Range(1, .ListColumns.Count)).Insert shift:=xlDown
                nRws = .ListRows(i).Range(1, .ListColumns.Count)
                rng.Cells(1, 4) = 1
                rng.Copy Destination:=rng.Offset(1).Resize(nRws)
            End If
        Next i
    End With
    Application.ScreenUpdating = True
End Sub

Open in new window


If you need any help where and how to use this code, just let me know.

Joop
0
 

Author Closing Comment

by:emiller1680
ID: 39236037
Joop,

Absolutely beautiful.  Thank you!

eric
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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,…
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

821 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