Hi Everyone,
I am using VBA to create a pivot table so far I automated the row labels to adjust but I am having trouble automating values to be added without manually going in and making the changes..
This is probably a simple fix but I am still relatively new to VBA..
Any help would be well appreciated
Code
Sub AddPivotFields()
Dim pvt As PivotTable
Dim dr As String
Dim dr_Name As String
Dim c As Range
Dim vcount As Long
Set pvt = ActiveSheet.PivotTables("PivotTable1")
pvt.PivotFields("Date").Orientation = xlRowField
pvt.PivotFields("Date").LayoutForm = xlTabular
pvt.PivotFields("Activity").Orientation = xlRowField
pvt.PivotFields("Activity").Caption = "Activity"
pvt.PivotFields("Activity").LayoutForm = xlTabular
pvt.PivotFields("Activity").Subtotals = _
Array(False, False, False, False, False, False, False, False, False, False, False, False)
pvt.PivotFields("Code").Orientation = xlRowField
pvt.PivotFields("Code").Caption = "Code"
pvt.PivotFields("Code").LayoutForm = xlTabular
'Direct and Indirect time do not change and are included within every report.
dr = "Direct"
dr_Name = "Direct Time"
pvt.AddDataField pvt.PivotFields("Direct"), dr_Name, xlSum
pvt.PivotFields("Direct Time").NumberFormat = "[h]:mm"
dr = "Indirect"
dr_Name = "Indirect Time"
pvt.AddDataField pvt.PivotFields("Indirect"), dr_Name, xlSum
pvt.PivotFields("Indirect Time").NumberFormat = "[h]:mm"
'I am having trouble automating this section I usually need to remove the comments with the code that are being used..
'like this example I am using A215 & B300 it would be nice if i could have an array with all the codes so it could check itself.. I tried but never worked..
dr = "A215"
dr_Name = "Avg A215"
pvt.AddDataField pvt.PivotFields("A215"), dr_Name, xlAverage
pvt.PivotFields("Avg A215").NumberFormat = "[h]:mm"
' dr = "A295"
' dr_Name = "Avg A295"
'
' pvt.AddDataField pvt.PivotFields("A295"), dr_Name, xlAverage
' pvt.PivotFields("Avg A295").NumberFormat = "[h]:mm"
' dr = "B200"
' dr_Name = "Avg B200"
'
' pvt.AddDataField pvt.PivotFields("B200"), dr_Name, xlAverage
' pvt.PivotFields("Avg B200").NumberFormat = "[h]:mm"
' dr = "B103"
' dr_Name = "Avg B103"
'
' pvt.AddDataField pvt.PivotFields("B103"), dr_Name, xlAverage
' pvt.PivotFields("Avg B103").NumberFormat = "[h]:mm"
dr = "B300"
dr_Name = "Avg B300"
pvt.AddDataField pvt.PivotFields("B300"), dr_Name, xlAverage
pvt.PivotFields("Avg B300").NumberFormat = "[h]:mm"
End Sub
Display More