Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Why can I run my excel macro directly but not from a button?

Posted on 2002-03-08
8
Medium Priority
?
479 Views
Last Modified: 2010-07-27
I have written a macro, changept, to restructure a pivot table.  The macro works when I run it directly from excel(tools> macro, etc) but when I create a button and call changept within the event code I get an error message.  Why?  Is this just (yet) another example of VBA not handling pivot tables very well or am I doing this the wrong way?


I'm including the code for completeness below but this question is more about the difference between running code from a button and running it directly.

Thanks in advance

sub changept()
ActiveSheet.PivotTables("PivotTable1").PivotSelect "", xlDataAndLabel
ActiveSheet.PivotTables("PivotTable1").AddFields RowFields:= _
    "Selected Price Bands EU", ColumnFields:="Month ", PageFields:=Array( _
    "Page Size", "Print Speed Colour", "Mono Speed Band", "Long Product Name", _
        "Memory - RAM (MB)", "Print Speed Black (ppm)", "Vendor ", _
        "Channel ", "Mono or Colour")

endsub

Private Sub CommandButton3_Click()
changept
End Sub
0
Comment
Question by:Tamalaine
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 6850027
What is the error?
0
 

Author Comment

by:Tamalaine
ID: 6850054
Runtime error 1004,Addfields method of PivotTable Class failed!
0
 

Author Comment

by:Tamalaine
ID: 6850084
Runtime error 1004,Addfields method of PivotTable Class failed!
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Tamalaine
ID: 6850094
Runtime error 1004,Addfields method of PivotTable Class failed!
0
 
LVL 4

Expert Comment

by:WolfgangKoenig
ID: 6850113
Try this:
sub changept()
ActiveCell.Activate
ActiveSheet.PivotTables("PivotTable1").PivotSelect "", xlDataAndLabel
ActiveSheet.PivotTables("PivotTable1").AddFields RowFields:= _
...

;)
WoK  
0
 

Author Comment

by:Tamalaine
ID: 6850121
I've just discovered that it was apparently an excel bug.  I was having the problems on excel 97 but it works fine on 2000!  That said, I'd still be interested in knowing why I can run the macro directly but not via the button in excel 97.  Is it just down to excel being buggy or are there proper reasons for it?
0
 
LVL 17

Accepted Solution

by:
calacuccia earned 400 total points
ID: 6850528
Tamalaine,

This is a design problem of Excel, and I believe the workaround proposed by WolfgangKoeing is working.

See this article

http://support.microsoft.com/default.aspx?scid=kb;EN-US;q177527
0
 

Author Comment

by:Tamalaine
ID: 6850678
That's what I needed.  Thanks for everyone's help!
0

Featured Post

Office 365 Training for Admins - 7 Day Trial

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
New style of hardware planning for Microsoft Exchange server.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question