+ Reply to Thread
Results 1 to 2 of 2

how do I hard code a variable range for a pivot table in vba?

  1. #1
    havocdragon
    Guest

    how do I hard code a variable range for a pivot table in vba?

    Hey all. I have noticed that Pivot Tables when programmed in visual basic
    seem to be more picky than some other code. And honestly I dont write much of
    the code myself but know where the key fields I need to change are. Here is
    my current code

    Range("A1").select
    Selection.CurrentRegion.Select

    ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:= _
    "'Report'!R1C1:R797C12").CreatePivotTable TableDestination:="", _
    TableName:="Report"

    now if this is hard coded it is going to select A$1$:L$797$ everytime. I
    want that to be the current region or selection, or utilize the
    selection.currentregion.select in that code itself so this will work no
    matter how many columns or rows of info there are, it will only create a
    pivot table based on those regions.

  2. #2
    Debra Dalgleish
    Guest

    Re: how do I hard code a variable range for a pivot table in vba?

    You could use a dynamic range as the pivot source, and refer to the
    range name in the code. There are instructions here:

    http://www.contextures.com/xlPivot01.html


    havocdragon wrote:
    > Hey all. I have noticed that Pivot Tables when programmed in visual basic
    > seem to be more picky than some other code. And honestly I dont write much of
    > the code myself but know where the key fields I need to change are. Here is
    > my current code
    >
    > Range("A1").select
    > Selection.CurrentRegion.Select
    >
    > ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:= _
    > "'Report'!R1C1:R797C12").CreatePivotTable TableDestination:="", _
    > TableName:="Report"
    >
    > now if this is hard coded it is going to select A$1$:L$797$ everytime. I
    > want that to be the current region or selection, or utilize the
    > selection.currentregion.select in that code itself so this will work no
    > matter how many columns or rows of info there are, it will only create a
    > pivot table based on those regions.



    --
    Debra Dalgleish
    Excel FAQ, Tips & Book List
    http://www.contextures.com/tiptech.html


+ 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