+ Reply to Thread
Results 1 to 2 of 2

VBA Type Mismatch Error when creating PivotCache

Hybrid View

  1. #1
    Registered User
    Join Date
    08-05-2010
    Location
    South Carolina
    MS-Off Ver
    Excel 2003
    Posts
    3

    VBA Type Mismatch Error when creating PivotCache

    Hi,

    I am trying to make a Pivot Table in Excel 2003 using VBA and am getting a run-time error 13 Type mismatch on the red line below where I am trying to create my Pivot Cache. Any thoughts?
    Sub MakePivotTable()
        Dim pt As PivotTable
        Dim strField As String
        Dim data As Worksheet
            Set data = Sheets("Data")
        Dim PTOutput As Worksheet
    
            Set PTOutput = Sheets.Add
        Dim PTCache As PivotCache
        Dim PRange As Range
        
        
        
        ' Find the last row with data
        Dim finalRow As Long
        finalRow = data.Cells(Application.Rows.count, 1).End(xlUp).row
        
        ' Find the last column with data
        Dim finalCol As Long
        finalCol = data.Cells(1, Application.Columns.count).End(xlToLeft).Column
        
        ' Find the range of the data
        Set PRange = data.Cells(1, 1).Resize(finalRow, finalCol)
    
        Set PTCache = ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:=PRange)        
        ' Create the pivot table
       Set pt = PTCache.CreatePivotTable(TableDestination:=PTOutput.Cells(1, 1), _
        TableName:="SamplePivot")
       
         
         ' Define the layout of the pivot table
         
        
         ' Set update to manual to avoid recomputation while laying out
        pt.ManualUpdate = True
        
        
       
        ' Set up the row fields
        
        pt.RowGrand = False
        pt.AddFields RowFields:=Array( _
            "Opportuntiy_Area", "Triggered?", "Priority")
        
        With pt.PivotFields("Triggered?")
            .PivotItems("-1").Visible = False
            .PivotItems("0").Visible = False
        End With
        pt.PivotFields("Triggered?").Subtotals = _
            Array(False, False, False, False, False, False, False, False, False, False, False, False)
        pt.PivotFields("Opportuntiy_Area"). _
            Subtotals = Array(False, False, False, False, False, False, False, False, False, False, _
            False, False)
        
       
        ' Set up the data fields
        With pt.PivotFields("Triggered?")
            .Orientation = xlDataField
            .Function = xlCount
            .Position = 1
        End With
        
      ' Now calc the pivot table
        pt.ManualUpdate = False
        
        
       End Sub
    Thanks!
    Last edited by romperstomper; 09-21-2010 at 03:00 PM. Reason: add code tags

  2. #2
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,976

    Re: VBA Type Mismatch Error when creating PivotCache

    What are the values of FinalRow and FinalCol?

    Note: you must use code tags when posting code on the forum; I have added them for you on this occasion but please ensure you use them in future.
    Everyone who confuses correlation and causation ends up dead.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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