Solved

Add rows below pivot tables

Posted on 2013-05-15
10
296 Views
Last Modified: 2013-05-15
I need a macro that would loop through all worksheets and if there is a pivot table or tables on the sheet that the last cell in the pivot table is touching another cell with information in it then the macro would insert 10 rows below the pivot table.

Thanks,
Montrof
0
Comment
Question by:montrof
  • 7
  • 3
10 Comments
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 39168561
Try this:
Sub PivotExpander()
    Dim ws As Worksheet
    Dim pt As PivotTable
    Dim rNext As Range
    For Each ws In ActiveWorkbook.Worksheets
        For Each pt In ws.PivotTables
            With pt.TableRange1
                If Application.CountA(.Offset(.Rows.Count).Resize(1)) Then .Offset(.Rows.Count).Resize(10).EntireRow.Insert
            End With
        Next pt
    Next ws
End Sub

Open in new window

0
 
LVL 1

Author Comment

by:montrof
ID: 39168575
That is great one last question is there a way to group that data and then close the group.

Thanks,
montrof
0
 
LVL 1

Author Comment

by:montrof
ID: 39168583
Sorry I think I need to be a little clearer.  I want to group the new rows and the pivot into one group and then close the group.
0
 
LVL 85

Accepted Solution

by:
Rory Archibald earned 500 total points
ID: 39168639
Try this:
Sub PivotExpander()
    Dim ws As Worksheet
    Dim pt As PivotTable
    Dim rNext As Range
    For Each ws In ActiveWorkbook.Worksheets
        For Each pt In ws.PivotTables
            With pt.TableRange1
                If Application.CountA(.Offset(.Rows.Count).Resize(1)) Then
                    .Offset(.Rows.Count).Resize(10).EntireRow.Insert
                    With pt.TableRange2
                        .Offset(-1).Resize(.Rows.Count + 11).EntireRow.Group
                        ws.Outline.ShowLevels 1
                    End With
                End If
            End With
        Next pt
    Next ws
End Sub

Open in new window


depending on existing groupings and whether you have pivot tables next to each other horizontally, this may not work!
0
 
LVL 1

Author Closing Comment

by:montrof
ID: 39168755
Always amazed at the level of help.  Thank you so much

Montrof
0
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.

 
LVL 1

Author Comment

by:montrof
ID: 39169164
Sorry one more question it seems like it is not adding the rows below a lot of the pivots.  I want it to add them for any pivot and maybe I miss stated that previously.  I would appreciate your help if you can.
0
 
LVL 1

Author Comment

by:montrof
ID: 39169258
One more thing if there is not 10 blank rows below the pivot table add them otherwise do nothing
0
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 39169447
The code only adds rows if there is data in the row immediately below the pivot since that is what you asked for.  :)
0
 
LVL 1

Author Comment

by:montrof
ID: 39169489
IS there a way I could change it to look for 10 blank rows and if not add them?  If needed I can open another question.  

Thanks,
Montrof
0
 
LVL 1

Author Comment

by:montrof
ID: 39169586
I figured it out thanks for the help, have a good night.

Montrof
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

Suggested Solutions

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,…
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…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

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

21 Experts available now in Live!

Get 1:1 Help Now