Hi Guys,

I have a problem with creating a code for Pivot Table. The problem is that I wish to add a Pivot Field to Data, but this field has to depend on what is within a particular cell of the workbook. For example, if the cell contains the text "January", then the PivotField to be added to the Data in the Pivot Table is to be "January". I have the following code which is giving me an error.

Sub UpdatePivot()
Dim ws As Worksheet, SrcData As String, pvtCache As PivotCache
Dim ws2 As Worksheet, ws3 As Worksheet
Dim pf As PivotField, pt As PivotTable, df As PivotField, str As String
'Set ws = ThisWorkbook.Worksheets("Lisun Data")
Set ws2 = ThisWorkbook.Worksheets("Cover")
Set ws3 = ThisWorkbook.Worksheets("Stockist")
Set pt = ws3.PivotTables("PivotTable3")
For Each pf In pt.ColumnFields
    pf.Orientation = xlHidden
Next pf
Set str = ws2.Cells(6, 1)
With pt.PivotFields(str)    'PROBLEM LIES HERE
    .Orientation = xlDataField
    .Function = xlSum
    .Position = 1
End With
End Sub
Much appreciated for your advice, thank you.