Issue with Pivot Destination and dynamic range

allExcell

Board Regular
Joined
Jan 8, 2013
Messages
71
Hi All,

I need your help on the below code. The code I've written to create a pivot is working fine so far, but I need some help to modify it a bit, and this is where I'm stuck now.

Code:
Option Explicit
Sub testme1()

Dim LastRow As Long
Dim FirstCol As Long
Dim LastCol As Long
Dim iCol As Long
Dim RowFieldArray() As String
Dim DataFieldArray() As String
Dim wks As Worksheet
Dim PT As PivotTable
Dim Pf As PivotField
Dim vRowFields() As Variant
Dim vDataFields() As Variant
vRowFields = Array("A", "B", "C", "D")
 
Set wks = Worksheets("Sheet1")

With wks
.Activate
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
FirstCol = 2 'skipping columns A
LastCol = 286
 

ReDim DataFieldArray(26 To 286)
For iCol = 26 To 286
DataFieldArray(iCol) = .Cells(1, iCol).Value
Next iCol

.Parent.PivotCaches.Add(SourceType:=xlDatabase, _
[B]SourceData:=wks.Range("Contents")).CreatePivotTable _
TableDestination:=Sheets("Pivot").Range("A5"), _
[/B]TableName:="PivotTable1", _
DefaultVersion:=xlPivotTableVersion10
End With

With Sheets("Pivot")
.Activate
Range("A5").Select

.PivotTables("PivotTable1").AddFields RowFields:=vRowFields
 
Dim i As Integer
    Dim iFieldMax As Integer
     On Error Resume Next
     ' Find the number of PivotFields
    iFieldMax = ActiveSheet.PivotTables("PivotTable1").PivotFields.Count
     
     ' Loop through the fields in the Pivot
    For i = 1 To iFieldMax
         
        With ActiveSheet.PivotTables("PivotTable1").PivotFields(i)
             
             'Set subtotal calculation to nothing
            .Subtotals = Array(False, False, False, False, False, False, False, False, False, False, False, False)
             
        End With
         
    Next i
For iCol = 26 To 286
With .PivotTables(1).PivotFields(iCol)
.Orientation = xlDataField
.Function = xlSum
End With
Next iCol
With .PivotTables(1).DataPivotField
.Orientation = xlColumnField
.Position = 1
End With
ActiveSheet.PivotTables(1).ColumnGrand = False
 
End With
End Sub

Here I want to make the pivot range dynamic. I'm not able to make the source data dynamic. Please see below, this is what I'm using at present.

Code:
[B]SourceData:=wks.Range("Contents")).CreatePivotTable [/B]

Again, the Pivot destination has to be hard coded with something like
Code:
[B]TableDestination:=Sheets("Pivot").Range("A5"), [/B]
. Unfortunately, this one is throwing an error, saying "Run-time error '1004'. Application-defined or object-defined error." while selecting below lines of code.
Code:
.Parent.PivotCaches.Add(SourceType:=xlDatabase, _
SourceData:=wks.Range("Contents")).CreatePivotTable _
TableDestination:=Sheets("Pivot").Range("A5"), _
TableName:="PivotTable1", _
DefaultVersion:=xlPivotTableVersion10

Any help would be appreciated.

Thanks in advance.
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Hi All,

Second part of the problem, i.e. hardcoding the pivot destination has been resolved. I've tweeked the code a bit and it worked well. Please see below:

Code:
[B]With Sheets("Pivot")
.Activate
[/B].Parent.PivotCaches.Add(SourceType:=xlDatabase, _
SourceData:=wks.Range("Contents")).CreatePivotTable _
TableDestination:=Sheets("Pivot").Range("A5"), _
TableName:="PivotTable1", _
DefaultVersion:=xlPivotTableVersion10
End With

Any help on creating a dynamic range would be of great help.
 
Upvote 0
Hi allExcel,

What do you mean by dynamic range? Does the named range "Contents" refer to a range address, or is it a formula?

Does that range have more than 65,536 rows?

For your "hardcoding the pivot destination" question that you solved, what is different about that part of your code in your Post #2 and Post #1?

Often, people get the error you describe when the PivotTable name specified already exists in that sheet.
 
Upvote 0
Hi Jerry,

For the pivot destination part, I've activated the Sheets("Pivot") before assigning the pivot destination. I thought it worked, as there was no other change in the code.

For the Dynamic range I mean something similar to the below code.

Code:
'LastCol = 286
'Set myRng = .Range("B11", .Cells(LastRow, LastCol))//'Set nRange = .Range("B11:IB" & LastRow).AutoFilter

The current named range "Contents" refer to a range address whoch gives a selection of some 50 odd rows with some 286 columns in it. There was no specific reason for doing this. This is only because I couldn't define a dynamic range.

Actually, it has to select rows having values from column B11 onwards till the last row with any value in it; and should not pick up rows having formulae in it, only rows with values to be picked.

I hope this answers all your questions. Please let me know if you need any more details.

Thanks & Regards.
 
Last edited:
Upvote 0
For a range of that size (not more than 65K rows), you should be able to assign that range to a variable and use it in your CreatePivotTable statement.

Your approach of defining your Pivot data source as a variable number of columns up until the point the cells go from having constants to formulas sounds overly complicated.

Is there a simpler way to define that last column of the data source column, such as the name of the field header?

Your adding the .Activate statement probably isn't what made the error message go away.
Try again with and without .Activate (making sure that you delete PivotTable1 each time before running).
 
Upvote 0
Hi Jerry, columns starting from 26 To 286 are having headers as "7-01-2012", "14-01-2012", "21-01-2012", etc....for next few years. SO, I thought the best way of defining thesm is by column numbers instead of headers. And source data sheets has formula in many of the columns which should not be selected while selecting the range, else Pivot will have multiple (blank) rows in them.
 
Upvote 0
Sorry, but I don't understand what you're doing. What makes the range go from having constant values to formulas?
Do you paste as values in a column for each date on a weekly basis and all future dates are formula?

A small screen shot showing the first few columns and the demising point between the constants and formulas would help clarify things.

As a general comment, databases typically work better if they have as few columns as practical even if it means having more rows and more total cells. You might consider revisiting the structure of your 50 Row by 286 Column data.
 
Upvote 0
Define Name by using "Name Manager" to your Pivot Table Source Data and write following formula in "Refers to" selection bar for dynamic range:

=OFFSET(Data!$A$1,0,0,COUNTA(Data!$A:$A),COUNTA(Data!$1:$1))
 
Upvote 0
Sorry, but I don't understand what you're doing. What makes the range go from having constant values to formulas?
Do you paste as values in a column for each date on a weekly basis and all future dates are formula? .

All the weeks starting from column AA till column KA are generated through formula. Constant values in headers are there till column Z.

A small screen shot showing the first few columns and the demising point between the constants and formulas would help clarify things.

As a general comment, databases typically work better if they have as few columns as practical even if it means having more rows and more total cells. You might consider revisiting the structure of your 50 Row by 286 Column data

There is not much I can do as the template is owned by someboday else, and I'm simply trying to make my work easier through this enhancement.

Also, I tried to attach a screenshot of the template but failed, as I'm not sure how to do that !! So, I've created one for your help. Let's assume, green highlighted rows/columns are the boundaries. From Header bb till dd are constant values this way till column Z all are constant values. Starting from Column AA are all formula driven headers. Rows starting from B11 are to be considered for the data range, as Column A is non editable and blank working as a margin. Again from B1 till B10 there are other details limited till F11 which are not needed in pivot.

Hope this helps.


[TABLE="class: grid, width: 500"]
<TBODY>[TR]
[TD]A
[/TD]
[TD]B
[/TD]
[TD]C
[/TD]
[TD]D
[/TD]
[TD]AA
[/TD]
[TD]AB
[/TD]
[TD]AC
[/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]A11
[/TD]
[TD]bb
[/TD]
[TD]cc
[/TD]
[TD]dd
[/TD]
[TD]7-01-2012
[/TD]
[TD]14-01-2012
[/TD]
[TD]21-01-2012
[/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</TBODY>[/TABLE]
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,599
Messages
6,179,828
Members
452,946
Latest member
JoseDavid

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top