Recorded macro does not play as recorded

I have a macro that generates 2 pivot tables on a worksheet called "EXTRAS"

Sub EXTRAS()
'
' Macro1 Macro
'
Dim rg As Range
Set rg = Range("A1").CurrentRegion
'
    ActiveCell.Select
    Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
    ActiveWorkbook.Names.Add Name:="data", RefersToR1C1:="='" & rg.Worksheet.Name & "'!" & rg.Address(ReferenceStyle:=xlR1C1)

    
    
    Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
    Sheets.Add.Name = "EXTRAS"
    ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
        "QRESULT!R1C1:R19C8", Version:=xlPivotTableVersion15).CreatePivotTable _
        TableDestination:="EXTRAS!R3C1", TableName:="PivotTable1", DefaultVersion _
        :=xlPivotTableVersion15
    Sheets("EXTRAS").Select
    Cells(3, 1).Select
    With ActiveSheet.PivotTables("PivotTable1").PivotFields("NAME_D")
        .Orientation = xlRowField
        .Position = 1
    End With
    ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
        "PivotTable1").PivotFields("NAME_D"), "Total Needed", xlCount
    
    
    
    ActiveCell.Offset(0, 3).Range("A1").Select
    ActiveWorkbook.Worksheets("EXTRAS").PivotTables("PivotTable1").PivotCache. _
        CreatePivotTable TableDestination:="EXTRAS!R3C4", TableName:="PivotTable2" _
        , DefaultVersion:=xlPivotTableVersion15
    Sheets("EXTRAS").Select
    Cells(3, 4).Select
    With ActiveSheet.PivotTables("PivotTable2").PivotFields("SHORTNAME")
        .Orientation = xlRowField
        .Position = 1
    End With
    With ActiveSheet.PivotTables("PivotTable2").PivotFields("NAME_D")
        .Orientation = xlRowField
        .Position = 2
    End With
    
End Sub

Open in new window


When I run this, I lose the row labels in PivotTable1 for some reason.

Attached is a sample file that you can run the macro on to see.

Here is a pic of what it SHOULD look like.
This is the desired pivot format - note the row labels in col ABook3.xlsm
mycomacAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

byundtMechanical EngineerCommented:
I definitely reproduce your problem.

I rewrote your EXTRAS macro to shorten it, make better use of the range variable rg, and eliminate the Activate and Select statements. It still had the same problem with no row labels on PivotTable1. So I then reversed the order of adding the data field and specifying the row items. That appeared to fix the problem.
Sub EXTRAS()
'
' Macro1 Macro
'
Dim rg As Range
Dim sDataAddress As String
Set rg = Range("A1").CurrentRegion
sDataAddress = "'" & rg.Worksheet.Name & "'!" & rg.Address(ReferenceStyle:=xlR1C1)
'
    ActiveWorkbook.Names.Add Name:="data", RefersToR1C1:="=" & sDataAddress
    
    Sheets.Add.Name = "EXTRAS"
    
    ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
        sDataAddress, Version:=xlPivotTableVersion15).CreatePivotTable _
        TableDestination:="EXTRAS!R3C1", TableName:="PivotTable1", DefaultVersion _
        :=xlPivotTableVersion15
    With ActiveSheet.PivotTables("PivotTable1")
        .AddDataField .PivotFields("NAME_D"), "Total Needed", xlCount
        .PivotFields("NAME_D").Orientation = xlRowField
        .PivotFields("NAME_D").Position = 1
    End With
    
    
    ActiveWorkbook.Worksheets("EXTRAS").PivotTables("PivotTable1").PivotCache. _
        CreatePivotTable TableDestination:="EXTRAS!R3C4", TableName:="PivotTable2" _
        , DefaultVersion:=xlPivotTableVersion15
    With ActiveSheet.PivotTables("PivotTable2")
        .PivotFields("SHORTNAME").Orientation = xlRowField
        .PivotFields("SHORTNAME").Position = 1
        .PivotFields("NAME_D").Orientation = xlRowField
        .PivotFields("NAME_D").Position = 2
    End With
    
End Sub

Open in new window

Book3_Q28736583.xlsm
0
mycomacAuthor Commented:
Doesn't fix the problem for me when I run it..
0
byundtMechanical EngineerCommented:
I just downloaded the workbook I posted, and ran the EXTRAS macro using 32-bit Excel 2013. It produced the results shown below.

Have you tried rebooting your computer or perhaps a different computer? There may be something about your install of Excel that prevents it from running like mine.
Results of EXTRAS macro
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mycomacAuthor Commented:
I'll try a reboot. I run Office 365
0
mycomacAuthor Commented:
Works great, less filling.  Thanks.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.