+ Reply to Thread
Results 1 to 6 of 6

Coding a dynamic data range for a pivot table

  1. #1
    Registered User
    Join Date
    06-28-2016
    Location
    Raleigh, NC
    MS-Off Ver
    MS Office 365 ProPlus
    Posts
    3

    Coding a dynamic data range for a pivot table

    I am trying to code a dynamic range for a pivot table. I have tried

    ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, _
    SourceData:=Sheets("FMRTReport.rdl").Range("$A$2:$V$" & LastRow), Version:=xlPivotTableVersion10). _
    CreatePivotTable TableDestination:="ActiveCharts!R1C1", TableName:="PivotTable2" _
    , DefaultVersion:=xlPivotTableVersion10

    where LastRow is a String variable initialized with

    LastRow = Cells(Rows.Count, "A").End(xlUp).Row

    I have also tried creating a variable defined as a range and substituting it in the formula. Nothing seems to be working. Can anyone help me? I am a basic user, so the easier the solution the better.

    Thanks so much!!!

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Coding a dynamic data range for a pivot table

    How is it not working?

    Errors? Nothing happening? Data not right?

    By the way, are you sure you are looking for the last row in column A on the right sheet?
    If posting code please use code tags, see here.

  3. #3
    Registered User
    Join Date
    06-28-2016
    Location
    Raleigh, NC
    MS-Off Ver
    MS Office 365 ProPlus
    Posts
    3

    Re: Coding a dynamic data range for a pivot table

    I get a Run-time error '13': Type mismatch. When I look at the value for LastRow it is set to the appropriate value based on the data in my spreadsheet.

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Coding a dynamic data range for a pivot table

    Doh, missed the 'String variable' bit.

    LastRow should be declared as Long (for Long Integer).

  5. #5
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,167

    Re: Coding a dynamic data range for a pivot table

    Hi Jemc and welcome to the forum,

    Have you read about Dynamic Named Ranges and how to use them?
    http://www.ozgrid.com/Excel/DynamicRanges.htm
    http://www.bettersolutions.com/excel...G820716330.htm

    Here is an old example I have to show how to do a DNR with Pivot Refresh.
    Change Event Refreshes Pivot Table with DNR.xlsm
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  6. #6
    Registered User
    Join Date
    06-28-2016
    Location
    Raleigh, NC
    MS-Off Ver
    MS Office 365 ProPlus
    Posts
    3

    Re: Coding a dynamic data range for a pivot table

    Sorry, Norie. Tried changing it to Long and it still didn't work.

    MarvinP, no I haven't read about them. Thanks!

+ 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. Change Pivot Table Data Source based on Dynamic Range
    By hamidxa in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-16-2014, 04:46 PM
  2. [SOLVED] Dynamic Range for Pivot Table
    By dinesh_ltjd in forum Excel General
    Replies: 2
    Last Post: 05-04-2013, 05:14 AM
  3. Making a dynamic range from a subset of data from datasource in a pivot table
    By Dag Lyster in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-03-2012, 03:49 AM
  4. Replies: 4
    Last Post: 06-25-2012, 03:25 PM
  5. VBA Coding for extracting data from a Pivot Table
    By hansonca1 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-14-2011, 01:49 PM
  6. Update Pivot table using a dynamic range for source data
    By Carl Thompson in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-02-2011, 08:20 AM
  7. Dynamic range for pivot table
    By webbug08 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 05-01-2009, 08:44 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