Solved

insert copied rows in Excel table based on value in column

Posted on 2013-06-10
2
466 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
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.

911 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

22 Experts available now in Live!

Get 1:1 Help Now