Here's the macro code:
Sub NameStateSum()
'
' NameStateSum Macro
' This macro runs the three submacros to create and format a pivot table
form the Names and Addresses workbook.
'
' Keyboard Shortcut: Ctrl+Shift+N
'
Application.Run "PERSONAL.XLSB!NameStateSum1"
Application.Run "PERSONAL.XLSB!NameStateSum2"
Application.Run "PERSONAL.XLSB!NameStateSum3"
ActiveWorkbook.RunAutoMacros Which:=xlAutoClose
End Sub
Sub NameStateSum1()
'
' NameStateSum1 Macro
' This macro will open the Names and Addresses workbook with the xource data
for PivotTable.
'
'
ChDir _
"C:\Documents and Settings\Ted Horsch\Desktop\MentorTraining
Exercise Files"
Workbooks.Open Filename:= _
"C:\Documents and Settings\Ted Horsch\Desktop\MentorTraining
Exercise Files\Names and Addresses.xlsx"
Workbooks.Add
ActiveWorkbook.SaveAs Filename:= _
"C:\Documents and Settings\Ted Horsch\Desktop\MentorTraining
Exercise Files\Summary by State.xlsx" _
, FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
End Sub
Sub NameStateSum2()
'
' NameStateSum2 Macro
' This macro creates a PivotTable of employee last names by state and counts
the employees in each state.
'
'
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"C:\Documents and Settings\Ted Horsch\Desktop\MentorTraining
Exercise Files\Names and Addresses.xlsx!ClientList" _
, Version:=xlPivotTableVersion12).CreatePivotTable
TableDestination:= _
"Sheet1!R1C1", TableName:="PivotTable3", DefaultVersion:= _
xlPivotTableVersion12
Sheets("Sheet1").Select
Cells(1, 1).Select
With ActiveSheet.PivotTables("PivotTable3").PivotFields("LastName")
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable3").PivotFields("State")
.Orientation = xlColumnField
.Position = 1
End With
ActiveSheet.PivotTables("PivotTable3").AddDataField
ActiveSheet.PivotTables( _
"PivotTable3").PivotFields("FirstName"), "Count of FirstName", xlCount
ActiveSheet.PivotTables("PivotTable3").TableStyle2 = "PivotStyleLight22"
ActiveSheet.PivotTables("PivotTable3").ShowTableStyleRowStripes = True
ActiveSheet.PivotTables("PivotTable3").RowGrand = False
End Sub
Sub NameStateSum3()
'
' NameStateSum3 Macro
' This macro renames the worksheet and saves the file.
'
'
Sheets("Sheet1").Select
Sheets("Sheet1").Name = "Summary by State"
ActiveWorkbook.Save
End Sub