Solved

insert copied rows in Excel table based on value in column

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

This is pretty cool.  The purpose of this VB Script is to help you document where JAR (Java ARchive) files and specifically java class files are located so that you can address issues seen with a client or that you can speak intelligently with a dev…
Approximate matching with VLOOKUP and MATCH seems to me to be a greatly under-used technique, and one which is vital for getting good performance out of large lookups. Until recently I would always have advised using an exact match for simplicity an…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
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…

705 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

18 Experts available now in Live!

Get 1:1 Help Now