Hi all,
I trying to build a macro that uses a Userform to collect information that resides on a datasheet, and then launches a pivot table to display this data. My current problem lies with trying to get the macro to add datafields to the pivot table (in this case, portfolio performance by year). As long as I specify which years to collect, the macro has no problem organizing the data. However, the number of years in this workbook is subject to change, and I'm trying to come up with a loop or something that will add these years automatically as they are added to the workbook.
This is an example of the code that's working:
This is what I'm trying to do:
Please let me know if I'm being too vague!
Thanks for all your help in advance
Marc
I trying to build a macro that uses a Userform to collect information that resides on a datasheet, and then launches a pivot table to display this data. My current problem lies with trying to get the macro to add datafields to the pivot table (in this case, portfolio performance by year). As long as I specify which years to collect, the macro has no problem organizing the data. However, the number of years in this workbook is subject to change, and I'm trying to come up with a loop or something that will add these years automatically as they are added to the workbook.
This is an example of the code that's working:
Code:
ActiveSheet.PivotTables("Tableau croisé dynamique").AddDataField ActiveSheet.PivotTables("Tableau croisé dynamique").PivotFields("Performance 2004 ( % )"), "Somme de Performance 2004 ( % )", xlSum
ActiveSheet.PivotTables("Tableau croisé dynamique").AddDataField ActiveSheet.PivotTables("Tableau croisé dynamique").PivotFields("Performance 2005 ( % )"), "Somme de Performance 2005 ( % )", xlSum
ActiveSheet.PivotTables("Tableau croisé dynamique").AddDataField ActiveSheet.PivotTables("Tableau croisé dynamique").PivotFields("Performance 2006 ( % )"), "Somme de Performance 2006 ( % )", xlSum
ActiveSheet.PivotTables("Tableau croisé dynamique").AddDataField ActiveSheet.PivotTables("Tableau croisé dynamique").PivotFields("Performance 2007 ( % )"), "Somme de Performance 2007 ( % )", xlSum
This is what I'm trying to do:
Code:
For i = 0 To étude.YearBox.ListCount
' where étude is a Userform, and YearBox is a ListBox
ActiveSheet.PivotTables("Tableau croisé dynamique").AddDataField ActiveSheet.PivotTables("Tableau croisé dynamique").PivotFields(Sheets("Data").Cells(i + 2, 1)), i, xlSum
Next i
Please let me know if I'm being too vague!
Thanks for all your help in advance
Marc