Solved

insert copied rows in Excel table based on value in column

Posted on 2013-06-10
2
493 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
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…

615 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