Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 374
  • Last Modified:

Pivot not working.

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
RWayneH
Asked:
RWayneH
  • 4
  • 4
1 Solution
 
als315Commented:
Try to change
Sheets.Add
to
Sheets.Add.Name = "Sheet5"
0
 
RWayneHAuthor Commented:
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
 
als315Commented:
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
RWayneHAuthor Commented:
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
 
als315Commented:
Can you show expected result?
0
 
RWayneHAuthor Commented:
0
 
als315Commented:
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
 
RWayneHAuthor Commented:
EXCELlent!!  Thanks. -R-
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now