You are here:

Excel/Updating Pivot Table

Advertisement


Question
QUESTION: Hi Tom,

I fixed last problem. The one about the rows. It was an error from the list I was copying such information. Thanks anyway.

Right now I'm developing a code that updates the values in a pivot table. Every time I update a worksheet ("Data May '08 - ..."), I add rows of information below the last written row. The pivot table I have for that must be updated, but the range increments in rows everytime there's an update.

Is there a way to look for the last written row in sheet ("Data May '08 - ...") and put that number in the pivot table source data?

My code looks like this:

   ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:= _
       "'Data May ''08 - ...'!R1C1:R2330C32").CreatePivotTable TableDestination:="" _
       , TableName:="PivotTable1", DefaultVersion:=xlPivotTableVersion10
   ActiveSheet.PivotTableWizard TableDestination:=ActiveSheet.Cells(3, 1)
   ActiveSheet.Cells(3, 1).Select

This value, R1C1:R2330C32, is the one that must be updated each time I run the macro. For example, if last written row is 2780, then this code must be written like: R1C1:R2780C32. Columns don't vary, so I'm only interested in the second R.

How can I do this?

Thanks for your help.

ANSWER: Rafael,

Dim rw as Long
with worksheets("Data May ''08 - ...'")
 set rw = .cells(rows.count,1).end(xlup).row
end with

ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, _
  SourceData:="'Data May ''08 - ...'!R1C1:R" _
     rw & "C32").CreatePivotTable _
     TableDestination:="", _
     TableName:="PivotTable1", _
     DefaultVersion:=xlPivotTableVersion10

ActiveSheet.PivotTableWizard _
  TableDestination:=ActiveSheet.Cells(3, 1)
  ActiveSheet.Cells(3, 1).Select

--
Regards,
Tom Ogilvy


---------- FOLLOW-UP ----------

QUESTION: Tom,

There's must be an error somewhere cause when I copied the code into the VB editor it displayed this part of the code in red:

ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, _
 SourceData:="'Data May ''08 - ...'!R1C1:R" _
    rw & "C32").CreatePivotTable _
    TableDestination:="", _
    TableName:="PivotTable1", _
    DefaultVersion:=xlPivotTableVersion10

Then, I changed my code to this:

   Sheets("Data May '08 - ...").Select
   Dim rw As Long
       With Worksheets("Data May ''08 - ...'")
       Set rw = .Cells(Rows.Count, 1).End(xlUp).Row
   End With
   ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:= _
       "'Data May ''08 - ...'!R1C1:R _ rw&C32").CreatePivotTable TableDestination:="" _
       , TableName:="PivotTable1", DefaultVersion:=xlPivotTableVersion10
   ActiveSheet.PivotTableWizard TableDestination:=ActiveSheet.Cells(3, 1)
   ActiveSheet.Cells(3, 1).Select
   ActiveWorkbook.ShowPivotTableFieldList = True

And when I run the macro a message box display a Compile error that says Object required, focusing in the line Set rw =.

What should I do?


Answer

Rafael,
There were a couple of typos on my part.  This compiled for me:


Dim rw As Long
With Worksheets("Data May ''08 - ...")
rw = .Cells(Rows.Count, 1).End(xlUp).Row
End With

ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, _
 SourceData:="'Data May ''08 - ...'!R1C1:R" & _
    rw & "C32").CreatePivotTable _
    TableDestination:="", _
    TableName:="PivotTable1", _
    DefaultVersion:=xlPivotTableVersion10

ActiveSheet.PivotTableWizard _
 TableDestination:=ActiveSheet.Cells(3, 1)
 ActiveSheet.Cells(3, 1).Select

If it says subscript out of range, check the name of the worksheet in Worksheets("Data May ''08 - ...")
since that would be the source of the problem - but as I said it compiled for me and the name I had on the tab is

Data May '08 - ...

--
Regards,
Tom Ogilvy
  


--
Regards,
Tom Ogilvy

About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here

All Answers

Answers by Expert:


Ask Experts

Volunteer


Tom Ogilvy

Expertise

Worked with the program for many years - provided assistance on MS Excel Newsgroups since 1997. Have received the Microsoft MVP award annually since 1999. I don't answer questions on using Excel in a browser Since I have no way to test this. Prefer not to answer charting questions. I consider myself to be particularly knowledgeable about using VBA internal to Excel but have no problems with formulas and pivot tables either.

Experience

Have Used Excel for 15 - 20 years. Answered in excess of 70,000 Excel related questions in MS Excel newsgroups. Unless obvious, please specify whether you want a worksheet function or macro/VBA solution.

Education/Credentials
BS General Engineering (concentration in Industrial Engineering) MS Operations Research Systems Analysis

©2009 About.com, a part of The New York Times Company. All rights reserved.