We help IT Professionals succeed at work.
Get Started

Pivot Table Creation with refrence value another sheet

Last Modified: 2017-04-20
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
Watch Question
Excel VBA Developer
Distinguished Expert 2018
This problem has been solved!
Unlock 1 Answer and 4 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE