+ Reply to Thread
Results 1 to 4 of 4

Cannot create a pivot table reference.

Hybrid View

  1. #1
    Registered User
    Join Date
    06-21-2009
    Location
    North Carolina, USA
    MS-Off Ver
    Excel 2003
    Posts
    2

    Cannot create a pivot table reference.

    I have the following code to create a pivot table from a data set. I get a Reference is Not Valid at the red line below. I know the fix must be simple, but I have hit a blank on the fix.

    Thanks in advance.


     Dim WSD As Worksheet
        Dim WSData As Worksheet
        Dim PTCache As PivotCache
        Dim PT As PivotTable
        Dim PRange As Range
        Dim FinalRow As Long
        Dim FinalCol As Long
        Set WSData = Worksheets("Counter Measures")
        Set WSD = Worksheets("Pivot")
    
        ' Delete any prior pivot tables
        For Each PT In WSD.PivotTables
            PT.TableRange2.Clear
        Next PT
    
        ' Define input area and set up a Pivot Cache
        FinalRow = WSData.Cells(Application.Rows.Count, 1).End(xlUp).Row
        FinalCol = WSData.Cells(1, Application.Columns.Count).End(xlToLeft).Column
        Set PRange = WSData.Cells(1, 1).Resize(FinalRow, FinalCol)
    ''''''''''''''''''''''''''''''''
        Set PTCache = ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:="=PRange")
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''
    
    I get a Reference is not Valid error at the next line.
    
        ' Create the Pivot Table from the Pivot Cache
        Set PT = PTCache.CreatePivotTable(TableDestination:=WSD.Cells(3, 1), TableName:="PivotTable4")
    Last edited by GoPack1; 06-21-2009 at 01:35 PM. Reason: Clarify the title

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Bug I can't figure out

    Hello GoPack1,

    Welcome to the Forum!

    Your post title should convey a brief description of what you need help with. Please do not use general words and phrases like "Help!", "Code doesn't work", "Urgent!", etc. Your present title doesn't tell anyone what your question is or what type of problems you are experiencing.

    A better title would be "Error - Reference Not Valid when creating a Pivot Table".

    As a new member, please take the time to read over the
    Forum Rules .
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Registered User
    Join Date
    06-21-2009
    Location
    North Carolina, USA
    MS-Off Ver
    Excel 2003
    Posts
    2

    Re: Bug I can't figure out

    Sorry.

    I am learning. I hope the new title is better. I do not know how to describe it any better.

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Cannot create a pivot table reference.

    Hello GoPack1,

    Thanks for changing the title. I didn't notice from the first post that you're a Tarheel. As if GoPack1 wasn't a give away! I grew up in Asheville.

+ 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