PC Review


Reply
Thread Tools Rate Thread

Adding PivotTable fields with a macro

 
 
kittronald
Guest
Posts: n/a
 
      1st Jul 2011
I'm trying to add PivotTable fields with a macro, but it keeps coming
out as follows:

With ActiveSheet.PivotTables("PivotTable2").PivotFields("Fruit")
.Orientation = xlRowField
.Position = 4
End With
With ActiveSheet.PivotTables("PivotTable2").PivotFields("Fruit")
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable2").PivotFields("Cost")
.Orientation = xlRowField
.Position = 5
End With
With ActiveSheet.PivotTables("PivotTable2").PivotFields("Cost")
.Orientation = xlRowField
.Position = 2
End With


How can I get rid of With and End With and still make it work ?


- Ronald K.
 
Reply With Quote
 
 
 
 
GS
Guest
Posts: n/a
 
      1st Jul 2011
In order to get rid of With...End With you need to fully qualify each
line of executable code. The purpose of With...End With constructs is
to reduce the amount of DOT processing, and thus render more efficient
code.

Try...

With ActiveSheet.PivotTables("PivotTable2")
With .PivotFields("Fruit")
.Orientation = xlRowField: .Position = 4
.Orientation = xlRowField: .Position = 1
End With
With .PivotFields("Cost")
.Orientation = xlRowField: .Position = 5
.Orientation = xlRowField: .Position = 2
End With
End With

--
Garry

Free usenet access at http://www.eternal-september.org
ClassicVB Users Regroup! comp.lang.basic.visual.misc


 
Reply With Quote
 
kittronald
Guest
Posts: n/a
 
      2nd Jul 2011
Garry,

Thank you, that worked like a charm !

The only thing I changed was the first .Orientation line for each
field since they appear to be unnecessary.



- Ronald K.
 
Reply With Quote
 
GS
Guest
Posts: n/a
 
      3rd Jul 2011
kittronald laid this down on his screen :
> Garry,
>
> Thank you, that worked like a charm !
>
> The only thing I changed was the first .Orientation line for each
> field since they appear to be unnecessary.
>
>
>
> - Ronald K.


You're welcome! -Always glad to help.

I didn't know why you had 2 .Orientation lines but was only interested
in abbreviating your code to the minimum necessary for the task. Now
it's, as you say, 2 lines less and so all the better!

--
Garry

Free usenet access at http://www.eternal-september.org
ClassicVB Users Regroup! comp.lang.basic.visual.misc


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Pivottable Sum of fields. Saviz Microsoft Excel Misc 1 17th Jul 2008 12:10 PM
Combing Fields in a PivotTable??? ddawg09 Microsoft Excel Discussion 2 7th Feb 2006 02:47 AM
pivottable calculated fields =?Utf-8?B?SGVscEFs?= Microsoft Excel Misc 3 8th Dec 2005 10:50 PM
Can't add row or column fields in PivotTable =?Utf-8?B?em9yZ2thbmc=?= Microsoft Access 2 11th Nov 2004 07:54 PM
pivottable page fields =?Utf-8?B?R3JhbnQ=?= Microsoft Excel Programming 1 16th Oct 2004 09:09 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:15 AM.