+ Reply to Thread
Results 1 to 2 of 2

Formatting - Calculated Field in Pivot Table.

Hybrid View

  1. #1
    Registered User
    Join Date
    08-18-2009
    Location
    Houston, Texas
    MS-Off Ver
    Excel 2003
    Posts
    23

    Formatting - Calculated Field in Pivot Table.

    I have a report that I am creating using a pivot table. Everything works great except formatting. The pivot table can be expanded and contracted to drill down into different layers and when this happens, formatting issues arise. To combat this, I use a button to run the macro at the bottom to derive the formatting from the source data. Unfortunately, there are calculated fields that do not have source data, and thus the formatting is still an issue.

    Two questions:

    How can I best fix the calculated field formatting issue?
    Is there an event I can tie the macro to that will run if fields are expanded or contracted?

    Thanks,
    Kurt

    Sub SourceFormat()
    
    Dim oPivotTable As PivotTable
    Dim oPivotFields As PivotField
    Dim oSourceRange As Range
    Dim strLabel As String
    Dim strFormat As String
    Dim i As Integer
    
    On Error GoTo Err1
    
    'Identify PivotTable and capture source Range
        Set oPivotTable = ActiveSheet.PivotTables(ActiveCell.PivotTable.Name)
        Set oSourceRange = Range(Application.ConvertFormula(oPivotTable.SourceData, xlR1C1, xlA1))
    
    'Refresh PivotTable to synch with latest data
        oPivotTable.PivotCache.Refresh
    
    'Start looping through the columns in source range
        For i = 1 To oSourceRange.Columns.Count
        
        'Trap the column name and number format for first row of the column
            strLabel = oSourceRange.Cells(1, i).Value
            strFormat = oSourceRange.Cells(2, i).NumberFormat
        
        'Now loop through the fields PivotTable data area
            For Each oPivotFields In oPivotTable.DataFields
       
            'Check for match on SourceName then appply number format if there is a match
                If oPivotFields.SourceName = strLabel Then
                oPivotFields.NumberFormat = strFormat
                
            'Bonus:  Change the name of field to Source Column Name
                oPivotFields.Caption = strLabel & " "
                End If
            
            Next oPivotFields
        Next i
    
    Exit Sub
    
    'Error stuff
    Err1:
    If Err.Number = 1004 Then
    MsgBox "Cursor must be inside of a pivot table."
    Else
    MsgBox Err.Number & vbCrLf & Err.Description
    End If
    
    End Sub

  2. #2
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,438

    Re: Formatting - Calculated Field in Pivot Table.

    yes. the sheet object has a pivot table update event

    Private Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable)
    
    End Sub
    Cheers
    Andy
    www.andypope.info

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Calculated pivot table field using pivot table data in calculation
    By BrittleStar in forum Excel Charting & Pivots
    Replies: 2
    Last Post: 12-10-2012, 05:35 PM
  2. Replies: 0
    Last Post: 06-26-2012, 09:06 PM
  3. Pivot Table - Calculated Field
    By Shijesh Kumar in forum Excel General
    Replies: 2
    Last Post: 09-20-2008, 11:02 AM
  4. [SOLVED] pivot table formulas for calculated field or calculated item
    By Vicky in forum Excel General
    Replies: 3
    Last Post: 06-06-2006, 12:10 AM
  5. [SOLVED] pb with calculated field in a pivot table
    By Valeria in forum Excel General
    Replies: 0
    Last Post: 04-03-2006, 09:45 AM

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