Hi,
I'm attempting to clean up some code to create a pivot table. From 25 columns of $, 25 columns of Units, I am trying to create 25 columns of Average Price. Apparently pivot tables don't support arrays, and references according to the error message I get but then I rarely give up without at least running through this board. Thanks. All suggestions welcome
This is the line I'll have to write 25x:
ActiveSheet.PivotTables("PT1").CalculatedFields.Add "ARP", "= 'I$1'/ IU1"
that I would love to get into some code like:
For x = 1 To 25
dollarname = "I$" & x
unitname = "IU" & x
calcvar = "= " & dollarname & "/" & unitname
ActiveSheet.PivotTables("PT1").CalculatedFields.Add "myname", calcvar
Next x
''''''''''''''''''''''''
here's the whole thing clean up a bit, the $ and U portion works fine, it's the Average Price at the end I'm stuck on:
Sub createpivot()
'pivotarea
ActiveSheet.PivotTableWizard SourceType:=xlDatabase, SourceData:= _
"DATA!R1C1:R10C85", TableDestination:="", TableName:="PT1"
ActiveSheet.PivotTableWizard TableDestination:="R1C1:R10C85"
ActiveSheet.PivotTables("PT1").AddFields RowFields:=Array("UPC", "Data")
Dim ColumnArray As Variant
ColumnArray = Array("UPC", "ITEM", "BRAND", "CONTENT", _
"SUBCATEGORY", "CATEGORY")
ActiveSheet.PivotTables("PT1").AddFields PageFields:=ColumnArray
'dollars
For x = 1 To 26
myfield = "I$" & x
myname = "$" & x
With ActiveSheet.PivotTables("PT1").PivotFields(myfield)
.Orientation = xlDataField
.Name = myname
.NumberFormat = "$#,##0"
.Function = xlSum
End With
Next x
'units, no problem
For x = 1 To 26
myfield = "IU" & x
myname = "U" & 6 x
With ActiveSheet.PivotTables("PT1").PivotFields(myfield)
.Orientation = xlDataField
.Name = myname
.NumberFormat = "#,##0"
.Function = xlSum
End With
Next x
'Average Price
For x = 1 To 26
myname = "ARP" & x
dollarname = "I$" & x
unitname = "IU" & x
calcvar = "= " & dollarname & "/" & unitname
ActiveSheet.PivotTables("PT1").CalculatedFields.Add "myname", calcvar
ActiveSheet.PivotTables("PT1").PivotFields(myname).Orientation = xlDataField
Next x
End Sub
I'm attempting to clean up some code to create a pivot table. From 25 columns of $, 25 columns of Units, I am trying to create 25 columns of Average Price. Apparently pivot tables don't support arrays, and references according to the error message I get but then I rarely give up without at least running through this board. Thanks. All suggestions welcome
This is the line I'll have to write 25x:
ActiveSheet.PivotTables("PT1").CalculatedFields.Add "ARP", "= 'I$1'/ IU1"
that I would love to get into some code like:
For x = 1 To 25
dollarname = "I$" & x
unitname = "IU" & x
calcvar = "= " & dollarname & "/" & unitname
ActiveSheet.PivotTables("PT1").CalculatedFields.Add "myname", calcvar
Next x
''''''''''''''''''''''''
here's the whole thing clean up a bit, the $ and U portion works fine, it's the Average Price at the end I'm stuck on:
Sub createpivot()
'pivotarea
ActiveSheet.PivotTableWizard SourceType:=xlDatabase, SourceData:= _
"DATA!R1C1:R10C85", TableDestination:="", TableName:="PT1"
ActiveSheet.PivotTableWizard TableDestination:="R1C1:R10C85"
ActiveSheet.PivotTables("PT1").AddFields RowFields:=Array("UPC", "Data")
Dim ColumnArray As Variant
ColumnArray = Array("UPC", "ITEM", "BRAND", "CONTENT", _
"SUBCATEGORY", "CATEGORY")
ActiveSheet.PivotTables("PT1").AddFields PageFields:=ColumnArray
'dollars
For x = 1 To 26
myfield = "I$" & x
myname = "$" & x
With ActiveSheet.PivotTables("PT1").PivotFields(myfield)
.Orientation = xlDataField
.Name = myname
.NumberFormat = "$#,##0"
.Function = xlSum
End With
Next x
'units, no problem
For x = 1 To 26
myfield = "IU" & x
myname = "U" & 6 x
With ActiveSheet.PivotTables("PT1").PivotFields(myfield)
.Orientation = xlDataField
.Name = myname
.NumberFormat = "#,##0"
.Function = xlSum
End With
Next x
'Average Price
For x = 1 To 26
myname = "ARP" & x
dollarname = "I$" & x
unitname = "IU" & x
calcvar = "= " & dollarname & "/" & unitname
ActiveSheet.PivotTables("PT1").CalculatedFields.Add "myname", calcvar
ActiveSheet.PivotTables("PT1").PivotFields(myname).Orientation = xlDataField
Next x
End Sub