Excel/Pivot Table

Advertisement


Question
QUESTION: Dear Tom

Thank you very much for your very prompt reply.  I have another question, which I hope you can answer.

The third and fourth line was generated by a macro recorder.  I modified the recording by adding line 1 and 2, but when I run the macro, I get run-time error 1004, 'Application-defined or object-defined error'.  Where did I go wrong? I am really very new to macro!

cCounter = "0016"
Sheets("Blotter").Select
ActiveSheet.PIVOTTABLES("PivotTable1").PivotFields("PRODCD").ClearAllFilters
ActiveSheet.PIVOTTABLES("PivotTable1").PivotFields("PRODCD").CurrentPage = _
   """ & cCounter & """

ANSWER: Francis Ang,

If I go into the immediate window in the VBE and execute your first line to set the value for cCounter and then test what you are assigning to the CurrentPage here is what I get

cCounter = "0016"
? """ & cCounter & """
" & cCounter & "

so I doubt that " & cCounter & " is what you were trying to achieve.

If you want to assign 0016  as the current page, then you can try

ActiveSheet.PIVOTTABLES("PivotTable1").PivotFields("PRODCD").CurrentPage = _
  cCounter


do if I test cCounter's value in the immediate window:

? cCounter
0016

Hopefully that will work for you.

--
Regards,
Tom Ogilvy


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

QUESTION: Dear Tom

I have another question.  I have the following codes -


ActiveSheet.PivotTables("PivotTable4").PivotCache.Refresh
ActiveSheet.PivotTables"PivotTable4").PivotFields ("PRODCD").ClearAllFilters
ActiveSheet.PivotTables("PivotTable4").PivotFields("PRODCD").CurrentPage = cCounter

When the value of the cCounter does not exist in my database, EXCEL would abend the process.  I tried to trap the error by using 'On Error Goto Handler' but it is not working.  How can I trap this error?

Please advise.  Thank you very much.

ANSWER: Hello Francis Ang,

The way I do it is to loop through the Pivotitems in that PivotField (PRODCD in your case) and compare each pivotitem value to the value of the cCounter variable.  If I find a match, I set the CurrentPage to the value of the Pivotitem (not the value of the cCounter Variable).  I have found this to be much more robust.  So if a match is not found, then no attempt is made to assign the currentpage value.

You are correct that assigning a non-existent value does not raise an error.  I found that it corrupts the pivottable, but that was in older versions of Excel.  That is why I have made sure the value is valid even to the extent to using the value of the PivotItem.

I haven't seen the term abend since the old IBM mainframe days.

--
Regards,
Tom Ogilvy


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

QUESTION: Dear Tom
Could you show me an example? I am not an IT person and have no formal training in programming, but have keen interest in writing program to make my work easier.  I learned through the help menu, internet search and seeking help from experts when everything else fails.

Answer
Francis Ang,

Dim pt as PivotTable
Dim pf as PivotField
Dim pvtitm as PivotItem

set pt = Activesheet.PivotTables("PivotTable4")
set pf = pt.PivotFields("PRODCD")
pt.PivotCache.Refresh
pf.ClearAllFilters
for each pvtitm in pf.PivotItems
 if pvtitm.Value = cCounter then
   pf.CurrentPage = pvtitm.Value
   exit for
 end if
Next

This is untested code (could contain errors/typos) - I just wrote it as an example since I don't  have a pivottable handy to benchtest it.  But it should represent a workable approach.

If you can't get it working, then send a sample file to twogilvy@msn.com  and I will take a look.  

--
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

Excel

All Answers


Answers by Expert:


Ask Experts

Volunteer


Tom Ogilvy

Expertise

Selected as an Excel MVP by Microsoft since 1999. Answering Excel questions in Allexperts since its inception in 2001. Able to answer questions on almost all aspects of Excel's internal capabilities. If seeking a VBA solution, please specify that in your question itself so I give you the answer you want. [Excel has weak protection - if you are distributing an application, I don't answer questions on how to protect your project from your users.]

Experience

Extensive experience.

Education/Credentials
Master of Science (MS) degree Operations Research (ORSA)

Awards and Honors
Microsoft MVP in Excel.

©2016 About.com. All rights reserved.