VBA Expand PivotTable if count is bigger than 10

th259

New Member
Joined
Oct 24, 2023
Messages
29
Office Version
  1. 365
Platform
  1. Windows
Uploaded is an image of a pivottable with a filter for 10 or more on the count field. How do I expand each of them to a new spreadsheet and name the new spreadsheet with just the first word on the Vendor Name column, and create new pivot table on the new worksheets like the one on the second image?
 

Attachments

  • VBA Expend Pivot Table.png
    VBA Expend Pivot Table.png
    32.2 KB · Views: 5
  • VBA Pivot Table for Most Used Vendors.png
    VBA Pivot Table for Most Used Vendors.png
    165.3 KB · Views: 6

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Upping this thread! I found the following code but I get a run-time error '1004'. One of the name was more than 31 characters. How do I change it so the worksheet name is only the first 20 characters or smaller if the name is shorter than that, and how do I change it so that the showdetails is only for visible rows or for row with count of 10 or more?

VBA Code:
Dim lCol As Long
    Dim c As Range
    Dim sRow As String
    sRow = "Row Labels"
    With ActiveSheet.PivotTables("multiple_lines")
        With .RowRange
            On Error Resume Next
            lCol = WorksheetFunction.Match(sRow, .Resize(1), 0)
            On Error GoTo 0
            If lCol = 0 Then
                MsgBox "Rowfield Header: " & sRow & "not found."
                Exit Sub
            End If
            lCol = .Column + lCol - 1
        End With
        For Each c In .DataBodyRange.Resize(, 1)
           c.ShowDetail = True
           ActiveSheet.Name = .Parent.Cells(c.Row, lCol)
        Next c
    End With
 
Upvote 0
I found a code that works but how do I get rid of the separate worksheet for the Grand Total?

VBA Code:
Dim lCol As Long
    Dim c As Range
    Dim sRow As String
    sRow = "Vendor Name"
    With ActiveSheet.PivotTables("multiple_lines")
        With .RowRange
            On Error Resume Next
            lCol = WorksheetFunction.Match(sRow, .Resize(1), 1)
            On Error GoTo 0
            If lCol = 0 Then
                MsgBox "Rowfield Header: " & sRow & "not found."
                Exit Sub
            End If
            lCol = .Column + lCol - 1
        End With
        For Each c In ActiveSheet.PivotTables("multiple_lines").DataBodyRange.Resize(, 1).SpecialCells(xlCellTypeVisible)
           c.ShowDetail = True
           ActiveSheet.Name = .Parent.Cells(c.Row, lCol)
        Next c
    End With
 
Upvote 0
Can it be as simple as adding the lines in blue ?

Rich (BB code):
        For Each c In ActiveSheet.PivotTables("multiple_lines").DataBodyRange.Resize(, 1).SpecialCells(xlCellTypeVisible)
           If c.Value <> "Grand Total" Then
                c.ShowDetail = True
                ActiveSheet.Name = .Parent.Cells(c.Row, lCol)
           End If
        Next c
 
Upvote 0
It didn't work, the macro is still running but the If code is not doing anything, I'm still getting a new worksheet for the Grand Total.
 
Upvote 0
Try this:

Rich (BB code):
        For Each c In ActiveSheet.PivotTables("multiple_lines").DataBodyRange.Resize(, 1).SpecialCells(xlCellTypeVisible)
           If c.Parent.Cells(c.Row, lCol) <> "Grand Total" Then
                c.ShowDetail = True
                ActiveSheet.Name = .Parent.Cells(c.Row, lCol)
           End If
        Next c
 
Upvote 0
Solution
It worked thank you very much!

To make it more complicated, how do I limit the vendor name to the first 20 characters.
 
Upvote 0
If you are just referring to the Vendor Name in creating the Sheet Names try this:

VBA Code:
                ActiveSheet.Name = Trim(Left(.Parent.Cells(c.Row, lCol), 20))
 
Upvote 0
I thought it would have been harder. It works, thank you very much!
 
Upvote 0

Forum statistics

Threads
1,215,110
Messages
6,123,148
Members
449,098
Latest member
Doanvanhieu

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top