langtu00189
Thành viên mới

- Tham gia
- 29/11/08
- Bài viết
- 25
- Được thích
- 0
Chào các bạn, mình có đọc qua bài viết tạo Pivot Table bằng VBA trong Excel 2000 tại đây www.giaiphapexcel.com/forum/showthread.php?556-D%C3%B9ng-VBA-trong-Excel-%C4%91%E1%BB%83-t%E1%BA%A1o-v%C3%A0-s%E1%BB%ADa-ch%E1%BB%AFa-PivotTable
Mình đã thử test lại với Excel 2007 thì bị lỗi ở chỗ bôi đỏ:
Các bạn có thể giải thích và giúp mình sửa lại cho đúng được không.
Mình xin gửi file đính kèm
Mình đã thử test lại với Excel 2007 thì bị lỗi ở chỗ bôi đỏ:
Mã:
Sub CreatePivotTable()Dim PTCache As PivotCache
Dim PT As PivotTable
Application.DisplayAlerts = False
'Xoa PivotSheet neu no ton tai'
On Error Resume Next
Application.DisplayAlerts = False
Sheets("PivotSheet").Delete
On Error GoTo 0
'Tao Pivot Cache'
Set PTCache = ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:=Sheets("sheet1").Range("A1").CurrentRegion.Address)
'Tao worksheet moi va dat ten'
Worksheets.Add
ActiveSheet.Name = "PivotSheet"
'Tao Pivot Table tu Cache'
Set PT = PTCache.CreatePivotTable(TableDestination:=Sheets("PivotSheet").Range("A1"), TableName:="PivotTable1")
With PT
'Them cac truong'
.PivotFields("region").Orientation = xlPageField
.PivotFields("Month").Orientation = xlColumnField
.PivotFields("SalesRep").Orientation = xlRowField
[COLOR=#ff0000].PivotFields("Sales").Orientation = xlDataField[/COLOR]
[COLOR=#ff0000].PivotFields("Target").Orientation = xlDataField[/COLOR]
'Them truong tinh toan'
.CalculatedFields.Add "Variance", "=Sales - Target"
.PivotFields("Variance").Orientation = xlDataField
'Thay doi caption'
.PivotFields("Sum of Sales").Caption = "Sales ($) "
.PivotFields("Sum of Target").Caption = "Target ($) "
.PivotFields("Sum of Variance").Caption = "Variance ($) "
'Thay doi caption'
Application.ScreenUpdating = True
End With
End Sub
Mình xin gửi file đính kèm