简体   繁体   中英

Use of variable in adding data to Values field of Pivot Chart using VBA

I have a data set which looks something like this Dataset However my number of days is a variable and will keep on changing.

I want to use VBA to create a Pivot Chart, the code as below:

Sheets("Initialize").Select
NoRes = Cells(6, 3).Value
Sheets("Metric Calculations").Select
ProjDur = Cells(31, 4).Value
Range(Cells(31, 61), Cells(30 + NoRes, 61 + ProjDur)).Select
Selection.Copy
Sheets("Resource Histogram Data").Select
Range("A2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
Range(Cells(1, 1), Cells(1 + NoRes, 1 + ProjDur)).Select
Application.CutCopyMode = False
r = 1 + NoRes
p = 1 + ProjDur
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=Sheets("Resource Histogram Data").Range(Cells(1, 1), Cells(r, p)), Version:=6). _
    CreatePivotTable TableDestination:="Sheet1!R1C1", TableName:="PivotTable1", DefaultVersion:=6
Sheets("Sheet1").Select
Cells(1, 1).Select
ActiveWorkbook.ShowPivotTableFieldList = True
ActiveSheet.Shapes.AddChart2(201, xlColumnClustered).Select
ActiveChart.SetSourceData Source:=Range("Sheet1!$A$1:$C$18")
With ActiveChart.PivotLayout.PivotTable.PivotFields("Resources")
    .Orientation = xlPageField
    .Position = 1
End With
    ActiveChart.PivotLayout.PivotTable.AddDataField ActiveChart.PivotLayout. _
        PivotTable.PivotFields("Day 1"), "Sum of Day 1", xlSum
    ActiveChart.PivotLayout.PivotTable.AddDataField ActiveChart.PivotLayout. _
        PivotTable.PivotFields("Day 2"), "Sum of Day 2", xlSum
    ActiveChart.PivotLayout.PivotTable.AddDataField ActiveChart.PivotLayout. _
        PivotTable.PivotFields("Day 3"), "Sum of Day 3", xlSum
ActiveChart.PivotLayout.PivotTable.AddDataField ActiveChart.PivotLayout. _
    PivotTable.PivotFields("Day 4"), "Sum of Day 4", xlSum
ActiveChart.PivotLayout.PivotTable.AddDataField ActiveChart.PivotLayout. _
    PivotTable.PivotFields("Day 5"), "Sum of Day 5", xlSum
ActiveChart.PivotLayout.PivotTable.AddDataField ActiveChart.PivotLayout. _
    PivotTable.PivotFields("Day 6"), "Sum of Day 6", xlSum
ActiveChart.PivotLayout.PivotTable.AddDataField ActiveChart.PivotLayout. _
    PivotTable.PivotFields("Day 7"), "Sum of Day 7", xlSum
ActiveChart.ShowValueFieldButtons = False

In the last part of the code in which the AddDataField is repeated for seven time. When the number of days will vary (between 1 to 200) this ideally should be in a for loop. However doing this obviously doesnt work.

For i = 1 To ProjDur    
    ActiveChart.PivotLayout.PivotTable.AddDataField ActiveChart.PivotLayout. _
    PivotTable.PivotFields("Day i"), "Sum of Day i", xlSum    
Next i

How do i get this for loop function included in my code?

It would be:

For i = 1 To ProjDur    
    ActiveChart.PivotLayout.PivotTable.AddDataField ActiveChart.PivotLayout. _
    PivotTable.PivotFields("Day " & i), "Sum of Day " & i, xlSum
Next i

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM