+ Reply to Thread
Results 1 to 1 of 1

How to select and count elements in a PivotTable with VBA

Hybrid View

  1. #1
    Registered User
    Join Date
    01-07-2013
    Location
    México
    MS-Off Ver
    Excel 2007
    Posts
    2

    How to select and count elements in a PivotTable with VBA

    Hello to all of you!

    I have this PivotTable PivotTable1.jpg where I need to count the number of dates for each month for certain customer ( In the example: 6 dates in the month "1" of the Customer named "Modular Tops, S. de R.L.". So far i have tried with this code:
    Sub 
    
    
    CalculateNoPayments()    ' columna H (8) OK
    
    Dim C, I As Integer
    Dim CTE, TD As String
    Dim WSD1 As Worksheet
    Dim WSD2 As Worksheet
    '
    '
    
    Set WSD1 = Worksheets("BASE")
    Set WSD2 = Worksheets("Days")
    
    CTE = WSD1.Range("AB1").Text
    TD = "TD3"
    
        WSD2.PivotTables(TD).PivotFields("Razon social").PivotItems(CTE).ShowDetail = True  "Customer detail"
        WSD2.PivotTables(TD).PivotFields("mes").ShowDetail = True   "Month Field detail"
        On Error Resume Next
        For I = 1 To 12
            WSD2.PivotTables(TD).PivotFields("mes").PivotItems(I).DataRange.Select                 'Even tried LabelRange, same result :(
                C = WSD2.PivotTables(TD).PivotFields("mes").PivotItems(I).DataRange.Count
    
                'Inserta valor de numero de pagos en tabla Base  "Inserts count on Sheet "BASE" for each month
                If C > 0 Then
                    WSD1.Cells(I + 3, 8).Value = C - 1
                    C = 0
                Else
                    WSD1.Cells(I + 3, 8).Value = "-"
                End If
        Next I
        On Error GoTo 0
        WSD2.PivotTables(TD).PivotFields("mes").ShowDetail = False
        WSD2.PivotTables(TD).PivotFields("Razon social").PivotItems(CTE).ShowDetail = False
        
        WSD1.Select
        Range("H4:H21").Select
        Selection.Style = "Comma"
        Selection.NumberFormat = "_-* #,##0_-;-* #,##0_-;_-* ""-""??_-;_-@_-"
        Range("A1").Select
    End Sub
    So far without any luck (I´m using Excel 2007). I have include the excel file with the data.

    ¿Could you help me find what I am missing? Thank you all in advance!

    Regards

    José Luis
    Attached Files Attached Files

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1