+ Reply to Thread
Results 1 to 10 of 10

Pivot cache using vba - Type mismatch error

  1. #1
    Registered User
    Join Date
    04-09-2015
    Location
    delhi
    MS-Off Ver
    7
    Posts
    15

    Pivot cache using vba - Type mismatch error

    Hello

    I have a code which is giving me an error, every time I try to create a pivot cache. The same code (with very slight changes) is working for another data sheet.

    I have not written the codes for inserting the pivot fields, since I am getting errors in this pivot cache for now. Please help.

    This is the code I have:

    Sub Comp_pvt()
    Dim PSheet As Worksheet 'pivot sheet and data sheet
    Dim PCache As PivotCache
    Dim PTable As PivotTable
    Dim PRange As Range ' define the range of the pivot table
    Dim LastRow As Long, LastCol As Long 'get the last row & column of our data range
    'Dim myfolder As String, myfile As String, filename As String

    Application.DisplayAlerts = False

    'insert a new sheet for the pivot table and the data sheet

    On Error Resume Next

    Sheets("Compliance Pivots").Delete

    On Error GoTo 0

    Sheets.Add.Name = "Compliance Pivots"

    Set PSheet = Sheets("Compliance Pivots") 'destination sheet for the pivot
    Set DSheet = Sheets("Comp_dedup") ' source sheet for the pivot

    LastRow = DSheet.Cells(Rows.Count, 1).End(xlUp).Row
    LastCol = DSheet.Cells(1, Columns.Count).End(xlToLeft).Column
    Set PRange = DSheet.Cells(1, 1).Resize(LastRow, LastCol)

    Set PCache = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=PRange)


    Set PTable = PCache.CreatePivotTable(tabledestination:=PSheet.Range("a2"), tablename:="Pivot")

    End Sub

  2. #2
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Pivot cache using vba - Type mismatch error

    Please review the forum rules
    Especially rule 3 regarding code tags.
    Also, can you tell us what line of your code generates the error?
    Please help by:

    Marking threads as closed once your issue is resolved. How? The Thread Tools at the top
    Any reputation (*) points appreciated. Not just by me, but by all those helping, so if you found someone's input useful, please take a second to click the * at the bottom left to let them know

    There are 10 kinds of people in this world... those who understand binary, and those who don't.

  3. #3
    Registered User
    Join Date
    04-09-2015
    Location
    delhi
    MS-Off Ver
    7
    Posts
    15

    Re: Pivot cache using vba - Type mismatch error

    This is where the code stops executing:

    Set PCache = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=PRange)

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

    Re: Pivot cache using vba - Type mismatch error

    How many rows of data do you have?

  5. #5
    Registered User
    Join Date
    04-09-2015
    Location
    delhi
    MS-Off Ver
    7
    Posts
    15
    Quote Originally Posted by rorya View Post
    How many rows of data do you have?
    The data has to be dynamic since it can change on a month on month basis.

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

    Re: Pivot cache using vba - Type mismatch error

    If you have a lot (more than 65536 rows) don't use a range object for the source, use an address string.

  7. #7
    Registered User
    Join Date
    04-09-2015
    Location
    delhi
    MS-Off Ver
    7
    Posts
    15
    Quote Originally Posted by rorya View Post
    If you have a lot (more than 65536 rows) don't use a range object for the source, use an address string.
    The data would be in 100s only. It wouldwould never go up to this much.

    I saw another code online, which was using the address. (,,,true) keyword. I used that and was able to pass the error issues. But what does that do?

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

    Re: Pivot cache using vba - Type mismatch error

    It does what you need and is the option I always use. However you should not have this issue with only a few hundred rows. I suspect your range is larger than you think - what's the value of lastRow?

  9. #9
    Registered User
    Join Date
    04-09-2015
    Location
    delhi
    MS-Off Ver
    7
    Posts
    15

    Re: Pivot cache using vba - Type mismatch error

    there are only 69 rows in the data sheet

  10. #10
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Pivot cache using vba - Type mismatch error

    If you can provide the workbook (sensitive data removed) maybe we can have a closer look. In principle your code should work, so the problem likely lies in the workbook itself, not the code logic.

+ 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. [SOLVED] Macro to update pivot tables and use the SAME cache instead of repeatedly recreating cache
    By Fowzee1 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-22-2015, 11:26 PM
  2. Get type mismatch error when trying to create pivot table
    By welchs101 in forum Excel Programming / VBA / Macros
    Replies: 16
    Last Post: 09-04-2013, 01:06 PM
  3. Pivot cache error
    By Ricardo Mass in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-26-2013, 05:06 AM
  4. Complile Error: Type Mismatch ??? After adding error trap
    By clemsoncooz in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-22-2012, 03:50 PM
  5. Conditional Formatting - Run Time Error '13' Type Mismatch Error
    By ksp in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-17-2011, 07:37 PM
  6. Help: Compile error: type mismatch: array or user defined type expected
    By lvcha.gouqizi in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-31-2005, 05:05 PM
  7. [SOLVED] error message with Pivot Cache
    By Wolfram Schneider in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-26-2005, 02:06 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