Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.excel.programming
|
|||
|
|||
Steps from Macro recorder for Pivot table will not run as a Macro
I am using Excel 97.
Background: I have a spreadsheet that, as part of a set of actions, I create a pivot table on. Due to this being a repetitive task by multiple users I would like to write VBA code to create the pivot table. This code would then be distributed as part of an addin for this set of actions (the rest of the actions and the addin have already been created - this would be an addition). Method: I turned on Macro recorder, created the pivot table (1 column field, 3 row fields, a sum data field), performed grouping on the column field, eliminated subtotals on the rows, eliminated grand totals on the columns then turned off the macro recorder. The pivot table on the screen was correct. The macro recorded code is below. Sub PivotMacro() ' Range("B3:T350").Select ActiveSheet.PivotTableWizard SourceType:=xlDatabase, SourceData:= _ "Jan2004min!R3C2:R350C20", TableDestination:="", TableName:="PivotTable2" ActiveSheet.PivotTables("PivotTable2").AddFields RowFields:=Array("Line", _ "MvT", "Length"), ColumnFields:="Pstg date" ActiveSheet.PivotTables("PivotTable2").PivotFields ("Pos Val").Orientation = _ xlDataField ActiveSheet.PivotTables("PivotTable2").PivotSelect "'Pstg date'[All]", _ xlLabelOnly Selection.Group Start:=True, End:=True, Periods:=Array(False, False, False, _ False, True, False, False) ActiveSheet.PivotTables("PivotTable2").PivotSelect "MvT", xlButton ActiveSheet.PivotTables("PivotTable2").PivotFields ("MvT").Subtotals = Array( _ False, False, False, False, False, False, False, False, False, False, False, False) ActiveSheet.PivotTables("PivotTable2").PivotSelect "Line", xlButton ActiveSheet.PivotTables("PivotTable2").PivotFields ("Line").Subtotals = Array( _ False, False, False, False, False, False, False, False, False, False, False, False) ActiveSheet.PivotTables("PivotTable2").PivotSelect "", xlDataAndLabel ActiveSheet.PivotTables("PivotTable2").ColumnGrand = False End Sub Problem: When I try to run this as a macro, it only goes so far then stops (no error messages). It creates the rows and columns fields but does not add the data field. If I divide it into six individual macros and run each separately, they will all work but the code will not work if ran as one macro (it seems it needs to be in the six individual parts). The six macro "steps" a 1. Create pivot table, row and column fields 2. Create data field 3. Group data field 4. Eliminate subtotals on first row field 5. Eliminate subtotal on second row field 6. Eliminate subtotal on columns Question: Is there something I am missing that is a way around this so that it can run in one macro? Or is it a function of Excel 97? (I recently had another problem where for Excel 97 I had to divide the steps into separate macros where for Excel 2002 it worked as one macro). Thanks for any help, Nancy |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
macro recorder and formulas | Excel Discussion (Misc queries) | |||
My excel macro recorder no longer shows up when recording macro | Excel Discussion (Misc queries) | |||
My excel macro recorder no longer shows up when recording macro | Excel Discussion (Misc queries) | |||
Macro recorder | Excel Discussion (Misc queries) | |||
Macro Recorder-Relative relation | Excel Discussion (Misc queries) |