Solved

Pivot not working.

Posted on 2013-12-27
8
354 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
  • 4
  • 4
8 Comments
 
LVL 39

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 39

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
 

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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 39

Expert Comment

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

Author Comment

by:RWayneH
ID: 39742565
0
 
LVL 39

Accepted Solution

by:
als315 earned 500 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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
PaperPort has a feature called the "Send To Bar". It provides a convenient, drag-and-drop interface for using other installed software, such as Microsoft Office. However, this article shows that the latest Office 2016 apps (installed with an Office …
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

706 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now