Evening
2 issues here (VBA code below)
I have a dynamic pivot table: 2 columns are added at the end ("Add New Date" and "Add Net Change Date") every time it's worked on.
A. offset issue
I can't work out the offset formula that will make it work. The data for one of DataFields needs to be: lastColumn.Offset(0, -2)
The VBA code I have works and doesn't bug but it doesn't pull this "famous" column. The Last Column is always ("Add Net Change Date")
B. Blank not removed
I have included for the blank cells to be removed, but they still appear in the PT. Any idea of why?
Could you someone help ? Thanks a lot for the help.
2 issues here (VBA code below)
I have a dynamic pivot table: 2 columns are added at the end ("Add New Date" and "Add Net Change Date") every time it's worked on.
A. offset issue
I can't work out the offset formula that will make it work. The data for one of DataFields needs to be: lastColumn.Offset(0, -2)
The VBA code I have works and doesn't bug but it doesn't pull this "famous" column. The Last Column is always ("Add Net Change Date")
B. Blank not removed
I have included for the blank cells to be removed, but they still appear in the PT. Any idea of why?
Could you someone help ? Thanks a lot for the help.
Code:
Sub PivotTableModel()
' PivotTable Macro
Dim objtable As PivotTable, objfield As PivotField
Dim NewCol&, LastRow&
Dim ws1 As Worksheet
Dim i As Long
Set ws1 = Worksheets("ACT HOURS VS BUDGET")
On Error Resume Next
'select sheet and 1st cell on the data table
ws1.Select
Range("A1").Select
'Create PivotTable based on the hour Datasheet
Set objtable = ws1.PivotTableWizard(Tabledestination:=Sheets.Add.Range("A1"))
'Specify row and Column fields.
With objtable
.PivotFields("MODEL").Orientation = xlRowField
.PivotFields("WC").Orientation = xlColumnField
lastColumn = ws1.Cells(1, Columns.Count).End(xlToLeft).Column
Selection.EntireColumn.Select
.PivotFields (.Range(lastColumn.Offset(0, -2))).Orientation = xlDataField
.PivotFields("Add New Date").Orientation = xlDataField
With .DataPivotField
.Orientation = xlColumnField
.Position = 2
End With
'loop through all fields in PivotTable,set subtotals to false
With objtable
For Each objfield In .PivotFields
objfield.Subtotals(1) = True
objfield.Subtotals(1) = False
Next objfield
objtable.ManualUpdate = False
End With
'Repeat Labels in 1st Row
objtable.RepeatAllLabels xlRepeatLabels
End With
'Remove Blank cells
With objtable
If .Rows.Count = 1 Then Exit Sub
Application.ScreenUpdating = False
For i = .Rows.Count To 2 Step -1
If .Cells(i, 1) = "" Or .Cells(i, 1) = "(blank)" Then _
.Cells(i, 1).EntireRow.Hidden = True
Next i
End With
Application.ScreenUpdating = True
End
End Sub
Last edited by a moderator: