Solved

VBA Excel - Pivot creation

Posted on 2011-02-15
2
465 Views
Last Modified: 2012-05-11
Dear Experts,

Could you please have a look at the attached file, on Sheet1 it contains a simple table and based on that trying to do a pivot.

I have two problems with it
1) somehow the macro always add a new sheet to the file, but I am not sure why
2) in the current format getting error message Run-time error 1004, Unable to get the PivotFields property of the PivotTable class at row <<    With ActiveSheet.PivotTables("PivotTable1").PivotFields("Qty") >>

Could you advise what causes these in Module1 macro?

thanks,
Sub PivotCreation()

'Count actual base sheet rows
Dim LastRowWithValue As Long
LastRowWithValue = ActiveSheet.UsedRange.Row - 1 + ActiveSheet.UsedRange.Rows.Count

'Source
     ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:= _
        "Sheet1!R1C1:R" & LastRowWithValue & "C2").CreatePivotTable TableDestination:="", _
        TableName:="PivotTable1"
'Place
    'ActiveSheet.PivotTableWizard TableDestination:=ActiveSheet.Cells(39, 1)
'or
    'ActiveSheet.PivotTableWizard TableDestination:=Sheet1.Cells(39, 1)
'or
    ActiveSheet.PivotTableWizard TableDestination:=Workbooks("PivotVBATemplate.xls").Worksheets("Sheet2").Cells(9, 1)
'Always
    ActiveSheet.PivotTables("PivotTable1").SmallGrid = False
'Column, Row fields
    ActiveSheet.PivotTables("PivotTable1").AddFields RowFields:= _
        "Item", ColumnFields:="Area"
'Data fields
    With ActiveSheet.PivotTables("PivotTable1").PivotFields("Qty")
        .Orientation = xlDataField
        .NumberFormat = "# ##0"
        .Function = xlSum
    End With
'Formatting, sum/count
'    ActiveSheet.PivotTables("PivotTable1").PivotFields( _
'       "Count of Qty").Function = xlSum
'Certain columns not visible
    With ActiveSheet.PivotTables("PivotTable1").PivotFields("Area")
        .PivotItems("West").Visible = False
    End With
'Columns replace and order
    ActiveSheet.PivotTables("PivotTable1").PivotFields("Area").PivotItems( _
        "South").Position = 1
'Paste special as values


End Sub

Open in new window

PivotVBATemplate.xls
0
Comment
Question by:csehz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 85

Accepted Solution

by:
Rory Archibald earned 500 total points
ID: 34898653
Your problems are both in lines 8-10. The C2 in the SourceData argument refers to column 2 (i.e. column B), so you are not including column C in the data, so you can't refer to Qty. Leaving the TableDestination argument blank means that Excel will always put it on a new sheet. You didn't say where you want it, so we can't fix it.
0
 
LVL 1

Author Closing Comment

by:csehz
ID: 34904553
Rory thanks I understand, so the error message problem can easily solve with including column C so applying C3.

And relating the additional sheet, better to define at that 8-10 lines at remove the 16
0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question