Steve Saunders
New Member
- Joined
- Jun 18, 2023
- Messages
- 12
- Office Version
- 365
- Platform
- Windows
Hi all,
I'm trying to create over 200 simple pivot tables using VBA. I'm pretty new to programming, but have found lots of good tutorials so have cobbled some code together.
The code asks for a range (I use the first row of column headers), creates a new worksheet based on the value in the cell in the range, and then inserts a pivot table on the sheet that has been created. Well that is how it is supposed to work.
The code stops after it creates the first sheet and doesn't give me any error. I'm guessing that my structure/flow isn't correct. The code to create the worksheets is OK, so the problem is either in the for loop and/or the code to create the pivot table. Can anyone see where it is breaking or are there commands to debug that I can use?
End Sub
Regards,
Steve
I'm trying to create over 200 simple pivot tables using VBA. I'm pretty new to programming, but have found lots of good tutorials so have cobbled some code together.
The code asks for a range (I use the first row of column headers), creates a new worksheet based on the value in the cell in the range, and then inserts a pivot table on the sheet that has been created. Well that is how it is supposed to work.
The code stops after it creates the first sheet and doesn't give me any error. I'm guessing that my structure/flow isn't correct. The code to create the worksheets is OK, so the problem is either in the for loop and/or the code to create the pivot table. Can anyone see where it is breaking or are there commands to debug that I can use?
VBA Code:
Sub InsertPivotTables()
'Dimension variables and declare data types for new worksheets
Dim rng As Range
Dim cell As Range
'Dimension variables and declare data types for pivot tables
Dim PSheet As Worksheet
Dim DSheet As Worksheet
Dim PCache As PivotCache
Dim PTable As PivotTable
Dim PRange As Range
Dim LastRow As Long
Dim LastCol As Long
'Enable error handling
On Error GoTo Errorhandling
'Prompt for a cell range
Set rng = Application.InputBox(Prompt:="Select cell range:", _
Title:="Create worksheets", _
Default:=Selection.Address, Type:=8)
'Iterate through cells in selected cell range
For Each cell In rng
'Check if cell is not empty
If cell <> "" Then
'Insert worksheet and name the worksheet based on cell value
Sheets.Add(After:=Sheets(Sheets.Count)).Name = cell
'Pivot table code
'Set worksheet to active sheet
Set PSheet = Worksheets(cell)
'Set data range - this is an existing sheet in the workbook
Set DSheet = Worksheets("Data")
'Define data range
LastRow = DSheet.Cells(Rows.Count, 1).End(xlUp).Row
LastCol = DSheet.Cells(1, Columns.Count).End(xlToLeft).Column
Set PRange = DSheet.Cells(1, 1).Resize(LastRow, LastCol)
'Define pivot cache
Set PCache = ActiveWorkbook.PivotCaches.Create _
(SourceType:=xlDatabase, SourceData:=PRange). _
CreatePivotTable(TableDestination:=PSheet.Cells(2, 2), _
TableName:=cell)
'Insert blank pivot table
Set PTable = PCache.CreatePivotTable _
(TableDestination:=PSheet.Cells(1, 1), TableName:=cell)
'Insert row fields
With ActiveSheet.PivotTables(cell).PivotFields(cell)
.Orientation = xlRowField
.Position = 1
End With
'Insert data field
With ActiveSheet.PivotTables(cell).PivotFields(cell)
.Orientation = xlDataField
.Function = xlSum
.NumberFormat = "#,##0"
.Name = "YOLO "
End With
End If
'Continue with next cell in cell range
Next cell
'Go here if an error occurs
Errorhandling:
End Sub
Regards,
Steve