Nhờ giúp đỡ: Pivot với VBA (1 người xem)

  • Thread starter Thread starter linhdt
  • Ngày gửi Ngày gửi
Liên hệ QC

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

linhdt

Thành viên chính thức
Tham gia
11/10/06
Bài viết
58
Được thích
108
Chào các bác
Em có lập trình một đoạn VBA để tạo Pivot, có 1 sheet DATA_MV là sheet dữ liệu, em đã tạo sẵn 1 sheet THMV để Pivot dữ liệu vào đó. Vấn đề là khi chạy code, dữ liệu Pivot lại không đưa được vào sheet THMV mà cứ nhảy ra thêm một sheet mới. Vậy nhờ các bác giúp em xem còn thiếu sót ở đoạn nào , bí quá e loay hoay cả tối chưa tìm ra. Em cảm ơn các bác.

Mã:
Sub Create_Pivot()
'On Error GoTo ErrorHandle
Dim wsData As Worksheet, wsPT As Worksheet
Dim PT_Cache As PivotCache
Dim PT As PivotTable
Dim LastRow As Long

'With ThisWorkbook
Set wsData = Worksheets("DATA_MV")
Set wsPT = Worksheets("THMV")

'End With

'// Khai bao dong cuoi cung dua vao cot C
LastRow = wsData.Cells(Rows.Count, 3).End(xlUp).Row

'// Xoa PivotTable cu
For Each PT In wsPT.PivotTables
    PT.TableRange2.Clear
Next PT
'//Tao Pivot Table Cache

    Set PT_Cache = ThisWorkbook.PivotCaches.Create(xlDatabase, wsData.Range("A1:O" & LastRow))
    
'//Tao Pivot Table
    Set PT = PT_Cache.CreatePivotTable(wsPT.Range("A1"), "THMuaVao")
    
    'Set PT = Sh15.PivotTables.Add(PivotCache:=PT_Cache, tabledestination:=Range("A1"))
    
        With PT
        '/ Thiet lap kieu
        .RowAxisLayout xlTabularRow
        .ColumnGrand = False
        .RowGrand = False
        '.tablestyle2= " "
        .HasAutoFormat = False
      
        '/Dong
            With .PivotFields("Don vi")
                .Orientation = xlRowField
                .Position = 1
                .LayoutBlankLine = False
                .Subtotals(1) = True
                
            End With
            With .PivotFields("Cong trinh")
                .Orientation = xlRowField
                .Position = 2
                .LayoutBlankLine = False
                .Subtotals(1) = False
            End With
          '/Gia tri
            With .PivotFields("HHMV chua VAT")
                .Orientation = xlDataField
                .Function = xlSum 'xlcount, xlaverage
                .Position = 1
                .NumberFormat = "#,##0"
            End With
            With .PivotFields("VAT")
                .Orientation = xlDataField
                .Function = xlSum 'xlcount, xlaverage
                .Position = 1
'                .NumberFormat = "#,##0"
            End With
       ' .PivotFields("Don vi").Orientation = xlRowField
       ' .PivotFields("Cong trinh").Orientation = xlRowField
       ' .PivotFields("HHMV chua VAT").Orientation = xlDataField
       ' .PivotFields("VAT").Orientation = xlDataField
        .PivotFields("Ki ke khai").Orientation = xlPageField
        .PivotFields("Tach HD").Orientation = xlPageField
        .DisplayFieldCaptions = False
        
        End With
'// Close cac bien khai bao
Set PT = Nothing
Set PT_Cache = Nothing
Set wsData = Nothing
Set wsPT = Nothing

   Columns("C:D").Select
    Selection.NumberFormatLocal = "#,##0"
 Exit Sub
ErrorHandle:
 MsgBox "Error: " & Err.Description, vbExclamation
End Sub
 
Chào các bác
Em có lập trình một đoạn VBA để tạo Pivot, có 1 sheet DATA_MV là sheet dữ liệu, em đã tạo sẵn 1 sheet THMV để Pivot dữ liệu vào đó. Vấn đề là khi chạy code, dữ liệu Pivot lại không đưa được vào sheet THMV mà cứ nhảy ra thêm một sheet mới. Vậy nhờ các bác giúp em xem còn thiếu sót ở đoạn nào , bí quá e loay hoay cả tối chưa tìm ra. Em cảm ơn các bác.
................................................................
Bạn tham gia từ năm 2006, mà sao vẫn chưa thuộc nội quy , vi phạm nội quy tại khoản 2, mục II. Hình thức của bài viết (do đăng 2 bài có cùng nội dung).
 
Upvote 0
Web KT

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

Back
Top Bottom