Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

insert copied rows in Excel table based on value in column

Posted on 2013-06-10
2
Medium Priority
?
503 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 2000 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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
After seeing numerous questions for Dynamic Data Validation I notice that most have used Visual Basic to solve the problem. This suggestion is purely formula based and can be used in multiple rows.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

916 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