trantuan11
New Member
- Joined
- Mar 27, 2016
- Messages
- 19
I have a code it doesn't work correctly, please help me
Sub addhideitems()
Dim WSD As Worksheet
Dim PTcache As PivotCache
Dim PT As PivotTable
Dim Prange As Range
Dim Finalrow As Long
Set WSD = Worksheets("sales")
Dim WSR As Worksheet
For Each PT In WSD.PivotTables
PT.TableRange2.Clear
Next PT
WSD.Range("u1:az1").EntireColumn.Delete
Finalrow = WSD.Cells(Rows.Count, 1).End(xlUp).Row
Finalcol = WSD.Cells(1, Columns.Count).End(xlToLeft).Column
Set Prange = Worksheets("sales").Cells(1, 1).Resize(Finalrow, Finalcol)
Set PTcache = ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:=Prange.Address)
Set PT = PTcache.CreatePivotTable(TableDestination:=WSD.Cells(2, Finalcol + 2), TableName:="PivotTablesales")
PT.ManualUpdate = True
PT.AddFields RowFields:="rEGION", ColumnFields:="date"
PT.PivotFields("REGION").CalculatedItems.Add "MyDivision", "='BAO LAM'+'BAO LOC'"
' Resequence so that the report has A292 and C409 first
PT.PivotFields("REGION").PivotItems("BAO LAM").Position = 1
PT.PivotFields("REGION").PivotItems("BAO LOC").Position = 2
PT.PivotFields("REGION").PivotItems("MyDivision").Position = 3
With PT.PivotFields("credit")
.Orientation = xlDataField
.Function = xlSum
.Position = 1
.NumberFormat = "#,##0"
End With
PT.NullString = "0"
With PT.PivotFields("Region")
End With
With PT.PivotFields("REGION")
.PivotItems("BAO LAM").Visible = False
.PivotItems("BAO LOC").Visible = False
End With
PT.ManualUpdate = False
PT.ManualUpdate = True
' Group OrderDate by Month and Year
PT.PivotFields("date").LabelRange.Group Start:=True, End:=True, _
Periods:=Array(False, False, False, False, True, False, True)
PT.ManualUpdate = False
PT.ManualUpdate = True
WSD.Activate
Range("J2").Select
End Sub
tthank
Sub addhideitems()
Dim WSD As Worksheet
Dim PTcache As PivotCache
Dim PT As PivotTable
Dim Prange As Range
Dim Finalrow As Long
Set WSD = Worksheets("sales")
Dim WSR As Worksheet
For Each PT In WSD.PivotTables
PT.TableRange2.Clear
Next PT
WSD.Range("u1:az1").EntireColumn.Delete
Finalrow = WSD.Cells(Rows.Count, 1).End(xlUp).Row
Finalcol = WSD.Cells(1, Columns.Count).End(xlToLeft).Column
Set Prange = Worksheets("sales").Cells(1, 1).Resize(Finalrow, Finalcol)
Set PTcache = ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:=Prange.Address)
Set PT = PTcache.CreatePivotTable(TableDestination:=WSD.Cells(2, Finalcol + 2), TableName:="PivotTablesales")
PT.ManualUpdate = True
PT.AddFields RowFields:="rEGION", ColumnFields:="date"
PT.PivotFields("REGION").CalculatedItems.Add "MyDivision", "='BAO LAM'+'BAO LOC'"
' Resequence so that the report has A292 and C409 first
PT.PivotFields("REGION").PivotItems("BAO LAM").Position = 1
PT.PivotFields("REGION").PivotItems("BAO LOC").Position = 2
PT.PivotFields("REGION").PivotItems("MyDivision").Position = 3
With PT.PivotFields("credit")
.Orientation = xlDataField
.Function = xlSum
.Position = 1
.NumberFormat = "#,##0"
End With
PT.NullString = "0"
With PT.PivotFields("Region")
End With
With PT.PivotFields("REGION")
.PivotItems("BAO LAM").Visible = False
.PivotItems("BAO LOC").Visible = False
End With
PT.ManualUpdate = False
PT.ManualUpdate = True
' Group OrderDate by Month and Year
PT.PivotFields("date").LabelRange.Group Start:=True, End:=True, _
Periods:=Array(False, False, False, False, True, False, True)
PT.ManualUpdate = False
PT.ManualUpdate = True
WSD.Activate
Range("J2").Select
End Sub
tthank
Last edited: