Hỏi về code tạo Pivot Table bằng VBA (1 người xem)

Liên hệ QC

Người dùng đang xem chủ đề này

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 đỏ:
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
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
 

File đính kèm

Kiểm tra lại tên cột là Target có 1 khoảng trắng đằng trước.
 
Upvote 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 đỏ:
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
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

Tham Khảo File này coi có áp dụng được gì không
 

File đính kèm

Upvote 0
Upvote 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 đỏ:
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
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

Do tiêu đề cột Target có khoảng trắng phía trước, sau khi sửa tiêu đề cột xong thì phải bỏ khoảng trắng trước chữ Target trong code của Macro2 thì mới hết lỗi, còn macro Pivottest1 tôi chưa rảnh sửa lại (vì đang trong giờ làm việc).

Lưu ý:
Để Macro tạo PivotTable chạy tốt thì tiêu đề cột không nên sử dụng tiếng Việt có dấu, kiểm tra lại để không có khoảng trắng phía trước tiêu đề cột và Copy tiêu đề cột xong Paste vào trong Code là chắc ăn nhất.
 
Lần chỉnh sửa cuối:
Upvote 0

Bài viết mới nhất

Back
Top Bottom