pivot update doesnt use new data

AnnetteTR

Board Regular
Joined
Aug 19, 2010
Messages
85
Hi I got a little stuck here

I have an excel file with data filled in a sheet with a Userform - hurra that works.
After having filled the sheet with data - it is time for calculating on the data and time to make overview over part of the data.

To that I have made a makro:

Code:
Sub AirVolumeOverview()
'
'
'
'    
Dim pt As PivotTable
Dim strField As String
Dim Sortering As String
Dim Forstevalg As String, Andetvalg As String, Tredjevalg As String, Fjerdevalg As String
Dim EnhedForstevalg As String, EnhedAndetvalg As String, EnhedTredjevalg As String, EnhedFjerdevalg As String
Dim Enhed1 As String, Enhed2 As String, Enhed3 As String, Enhed4 As String
 
'changes to the actual sheet
Sheets("Ventilation i rum").Select
 
'move to the topleft cell
Selection.End(xlToLeft).Select
Selection.End(xlUp).Select
 
' Naming the dataarea Items
Range(Selection.End(xlToRight), Selection.End(xlDown)).Name = "Items"
 
   Sortering = "Etage"
   Forstevalg = "Rum nr."
   Andetvalg = "Basis luftmængde [m³/h]"
   Tredjevalg = "Variabel luftmængde min"
   Fjerdevalg = "Variabel Luftmængde max"
 
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, _
  SourceData:="Items").CreatePivotTable TableDestination:="", TableName:="LOlist"
 
Set pt = ActiveSheet.PivotTables("LOlist")
'Placering af Pivottabel
ActiveSheet.PivotTableWizard TableDestination:=Cells(3, 1)
pt.AddFields RowFields:=Sortering    
 
With pt.PivotFields(Sortering)
    .Orientation = xlRowField
    .Position = 1
End With
 
   pt.AddDataField pt.PivotFields(Andetvalg), " " & Andetvalg, xlSum
   pt.AddDataField pt.PivotFields(Tredjevalg), " " & Tredjevalg, xlSum
   pt.AddDataField pt.PivotFields(Fjerdevalg), " " & Fjerdevalg, xlSum
 
With pt.PivotFields(Forstevalg)
    .Orientation = xlRowField
    .Position = 2
End With
 
'Formatting the valuearea
With pt.PivotFields(" " & Andetvalg)
        .NumberFormat = "0 ""m³/h"""
End With
 
With pt.PivotFields(" " & Tredjevalg)
        .NumberFormat = "0 ""m³/h"""
End With
With pt.PivotFields(" " & Fjerdevalg)
        .NumberFormat = "0 ""m³/h"""
End With
 
'Check if sheetname is used, puts the new sheet last in the row of sheets
Dim sht As Worksheet
For Each sht In ActiveWorkbook.Sheets
    If sht.Name Like "AirVolumeOverview*" Then arknr = arknr + 1
Next sht
ActiveSheet.Name = "AirVolumeOverview" & arknr + 1
ActiveSheet.Move after:=Worksheets(Worksheets.Count)
End Sub

Seamingly this works fine - but now I have added some data.
And it is not used in the pivottable.
As you can see from the code - the range to use for the pivot is changed accordingly.

I have tried to refresh manually. It doesnt make any difference. Only if I reduce the area with one line - the pivot is updated.

Is there a way to clear the Cashe an start over when the macro is run?

Hope for help
Regards Annette
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
1st thing I'd check on PT is the Pivot Source Data range - if range is set for specific rows, that is your problem & you can fix by changing to $A:$X (without row numbers included in range).
 
Upvote 0
Hi rallcorn

The Pivot Source Data range is set like this:
partly taken from my original question
Code:
' Naming the dataarea Items
Range(Selection.End(xlToRight), Selection.End(xlDown)).Name = "Items"
When I check - the new area is also using the new data.

So I define the area each time the macro runs.

Then I use it like this:
Code:
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, _
  SourceData:="Items").CreatePivotTable TableDestination:="", TableName:="LOlist"
Somehow it seems that the macro is using the old definition of "Items"

Regards Annette
 
Upvote 0
Did you manually check the actual pivot table itself (outside of VBA code) for the range?

First I'd get it working right when you manually add data & refresh PT, etc., then when you know it is all working OK step through the code a line at a time to see where you encounter the problem.
 
Upvote 0
Hi rallcorn<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
I have tried to "change source data" to same range as "Items" except for one row - then all the data is used. But when I change it back to "Items" it uses the old range.
<o:p></o:p>
When I check in "NameManager" the range for "Items" is correct.<o:p></o:p>

I have tried to manually make a new PT with "Items" as source data - it still uses the old definition for "Items" not the actual one.<o:p></o:p>

It seems that "Items" is not cleared correctly before the next run?<o:p></o:p>

Maybe the code should be changed to something like this:<o:p></o:p>
Code:
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, _
  SourceData:= [COLOR=red]Range(Selection.End(xlToRight), Selection.End(xlDown))[/COLOR]) _
.CreatePivotTable TableDestination:="", TableName:="LOlist"<o:p></o:p>
instead of using a named range.
<o:p></o:p>
I have also tried to copy sheets with constants, forms and modules to a new file without data.<o:p></o:p>

Then it seemingly works, but I haven’t tried to add and delete data yet.

Regards Annette
Using Excel 2007
 
Upvote 0
It works :laugh:

Thankyou for your inspiration.

Regards Annette
AnnetteTR may ask how solve that problem because i'm trying it to but it doenst work.
even try it with a record macro still getting error
Code:
ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:= _
"Database").CreatePivotTable TableDestination:="", TableName:= _
"PivotTable12", DefaultVersion:=xlPivotTableVersion10
ActiveSheet.PivotTableWizard TableDestination:=ActiveSheet.Cells(3, 1)
ActiveSheet.Cells(3, 1).Select
ActiveWorkbook.ShowPivotTableFieldList = True
With ActiveSheet.PivotTables("PivotTable12").PivotFields("Name")
.Orientation = xlRowField
.Position = 1
 
Upvote 0
Hi Drumaster

I did as I suggested in my mail of march 1st - marked with red.
It corresponds to replace "Database" in your code with the code you use to name the area "Database" (which you dont show).

Regards
Annette
 
Upvote 0
Hi Drumaster

I did as I suggested in my mail of march 1st - marked with red.
It corresponds to replace "Database" in your code with the code you use to name the area "Database" (which you dont show).

Regards
Annette

Thank you for you fast respone :)
But i did that to
Code:
ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:Range(Selection.End(xlToRight), Selection.End(xlDown))) _.CreatePivotTable TableDestination:="", TableName:= _
        "PivotTable12", DefaultVersion:=xlPivotTableVersion10
    ActiveSheet.PivotTableWizard TableDestination:=ActiveSheet.Cells(3, 1)
    ActiveSheet.Cells(3, 1).Select
    ActiveWorkbook.ShowPivotTableFieldList = True
    With ActiveSheet.PivotTables("PivotTable12").PivotFields("Name")
        .Orientation = xlRowField
        .Position = 1

I thought maybe because i have ActiveWorkbook.PivotCaches.Add i even change that to create Pivotcaches still a syntax error.

Or do i miss something.........

oh yeah i using Excel 2003 can that maybe it ??
 
Last edited:
Upvote 0
Hi Drummaster
I think you might have forgotten the equal sign
like:

Code:
ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:[SIZE=4][COLOR=red][B]=[/B][/COLOR][/SIZE]Range(Selection.End(xlToRight), Selection.End(xlDown))) _.CreatePivotTable TableDestination:="", TableName:= _
        "PivotTable12", DefaultVersion:=xlPivotTableVersion10
Hope that helps.

When the text is on same line you dont need Space Underscore " _" this is used to tell vba that you are changing line but is in the same command.
(I dont know if it is a problem that it is there!)

Regards
Annette
I am using excel 2007
 
Upvote 0

Forum statistics

Threads
1,224,527
Messages
6,179,328
Members
452,907
Latest member
Roland Deschain

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