?
Solved

Pivot not working.

Posted on 2013-12-27
8
Medium Priority
?
370 Views
Last Modified: 2013-12-28
For some reason this Pivot is not working.  Any ideas why this would not preform the way it was recorded?  It should create a sheet tab "Sheet5" everytime and it is not.  There are four other sheet tabs in this file.  Thanks  -R-


Sub Pivot1()

    Columns("A:A").Select
    Sheets.Add
    ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
        "Open Orders!R1C1:R1048576C1", Version:=xlPivotTableVersion12). _
        CreatePivotTable TableDestination:="Sheet5!R3C1", TableName:="PivotTable8" _
        , DefaultVersion:=xlPivotTableVersion12
    Sheets("Sheet5").Select
    Cells(3, 1).Select
    With ActiveSheet.PivotTables("PivotTable8").PivotFields("SOSOLN")
        .Orientation = xlRowField
        .Position = 1
    End With
    ActiveSheet.PivotTables("PivotTable8").AddDataField ActiveSheet.PivotTables( _
        "PivotTable8").PivotFields("SOSOLN"), "Count of SOSOLN", xlCount

Open in new window

Pivot1.xlsx
0
Comment
Question by:RWayneH
[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
  • 4
  • 4
8 Comments
 
LVL 40

Expert Comment

by:als315
ID: 39742359
Try to change
Sheets.Add
to
Sheets.Add.Name = "Sheet5"
0
 

Author Comment

by:RWayneH
ID: 39742384
No luck... it creates the sheet tab fine... it however does not put the SOSOLN in the rows of the pivot table, only in the column as a Count... -R-
0
 
LVL 40

Expert Comment

by:als315
ID: 39742420
Your question was:
It should create a sheet tab "Sheet5" everytime and it is not.
Do you like to get on inserted sheet pivot table from sheet6 in your sample?
0
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 

Author Comment

by:RWayneH
ID: 39742472
If I create the workbook in sequence it does create Sheet5, even with your edit... however look at what the pivot looks like.... it does not honor the SOSOLN into the rows... as the recorder does it.  Something is going on in the using of the same field as rows and values..

Please run the code and you will see what I mean.  (at least is has not been  working for me).
PivotError.jpg
0
 
LVL 40

Expert Comment

by:als315
ID: 39742544
Can you show expected result?
0
 

Author Comment

by:RWayneH
ID: 39742565
0
 
LVL 40

Accepted Solution

by:
als315 earned 2000 total points
ID: 39743226
Macro recorder very often skip some steps, so you should add them manually.
You can find guide here:
http://msdn.microsoft.com/en-us/library/hh243933(v=office.14).aspx
This code should work:
Sub Pivot1()
Dim objTable As Object
    Sheets("Open Orders").Activate
    Range("A1").Select
    Range(Selection, Selection.End(xlDown)).Name = "SOSOLNRange"
    Sheets.Add.Name = "Sheet5"
    Sheets("Sheet5").Activate
    ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
        "SOSOLNRange", Version:=xlPivotTableVersion12).CreatePivotTable TableDestination:="Sheet5!R3C1", TableName:="PivotTable8" _
        , DefaultVersion:=xlPivotTableVersion12
    Set objTable = ActiveSheet.PivotTables("PivotTable8")
    Set objField = objTable.PivotFields("SOSOLN")
    objField.Orientation = xlRowField
    Set objField = objTable.PivotFields("SOSOLN")
    objField.Orientation = xlDataField
    objField.Function = xlCount
End Sub

Open in new window

0
 

Author Closing Comment

by:RWayneH
ID: 39743378
EXCELlent!!  Thanks. -R-
0

Featured Post

Office 365 Training for Admins - 7 Day Trial

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

Question has a verified solution.

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

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
Ever visit a website where you spotted a really cool looking Font, yet couldn't figure out which font family it belonged to, or how to get a copy of it for your own use? This article explains the process of doing exactly that, as well as showing how…
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

770 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