troubleshooting Question

Pivot Table Creation with refrence value another sheet

Avatar of Vijay P
Vijay P asked on
Microsoft Office* PowerPivotMicrosoft ExcelVBA
4 Comments1 Solution111 ViewsLast Modified:
Dear experts,
Need to add pivot table as mentioned in below table with another sheet data range.

item      Pivot                DataFields
Date      xlRowField      
Time      xlRowField      
Band      xlColumnField      
Pencil      xlDataField      xlSum
Eraser      xlDataField      xlAverage
Paper      xlDataField      xlAverage
Note      xlDataField      xlSum

 i  am getting error in below lines
  pf.Orientation = MyArray(iCount, 2)
      pf.Function = MyArray(iCount, 3).Value

complete code

Dim pt As PivotTable

'The array is declared as a Variant, so it can contain
'different data types like e.g. numbers and tekst.
Dim MyArray(1 To 10, 1 To 3) As Variant
Dim rRange As Range
Dim iCount As Integer
Dim iCount2 As Integer

'We set our range = cell A1:A10
Set rRange = sheets("Sheet2").Range("A1:A10")

'Values are read from cell A1:A10 and to the right.
For iCount = 1 To 10
   With rRange.Item(iCount)
      MyArray(iCount, 1) = .Value
      MyArray(iCount, 2) = .Offset(0, 1).Value
      MyArray(iCount, 3) = .Offset(0, 2).Value
   End With

Dim pf As PivotField

Set pt = ActiveSheet.PivotTables(1)
'We now write the values back to the spreadsheet, but in reversed order.
For iCount = 10 To 1 Step -1
   iCount2 = iCount2 + 1
   With rRange.Item(iCount2)
   For Each pf In pt.PivotFields
    If pf.Name = MyArray(iCount, 1) Then
      pf.Orientation = MyArray(iCount, 2)
      pf.Function = MyArray(iCount, 3).Value
    End If
  Next pf
   End With
Set rRange = Nothing
Excel VBA Developer
Join our community to see this answer!
Unlock 1 Answer and 4 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 4 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros