'Trying to Make Pivot Tables with Excel Macros
I am trying to make a macro that I can use on any sheet of a workbook to sort a list of products and alternate highlighting based on product name. After this I want to make a pivot table of that data, but my macro only works on the original sheet I made it on. I am just learning VBA so I was only able to get this somewhat working using the record macro tool. When running on other sheets in the same workbook I get an Error 5 on line 106, and when running on other workbooks I get a Run-time error 91 on line 28. Any Assistance would be greatly appreciated!
Sub SplitSheetMaker()
'
' SplitSheetMaker Macro
' Splits products and highlights them based on product name and makes a pivot table.
'
'
Columns("D:D").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("D1").Select
ActiveCell.FormulaR1C1 = "0"
Range("D2").Select
ActiveCell.FormulaR1C1 = "=IF(RC[-3]=R[-1]C[-3],R[-1]C,1-R[-1]C)"
Range("D2").Select
Selection.AutoFill Destination:=Range("D2:D3880"), Type:=xlFillDefault
Range("D2:D3880").Select
ActiveWindow.ScrollRow = 3848
ActiveWindow.ScrollRow = 3836
ActiveWindow.ScrollRow = 3792
ActiveWindow.ScrollRow = 3482
ActiveWindow.ScrollRow = 2948
ActiveWindow.ScrollRow = 1012
ActiveWindow.ScrollRow = 311
ActiveWindow.ScrollRow = 1
Range("A1:E3880").Select
Range("C7").Activate
Selection.AutoFilter
ActiveWorkbook.Worksheets("ExampleSupplier").AutoFilter.Sort.SortFields.Clear
ActiveWorkbook.Worksheets("ExampleSupplier").AutoFilter.Sort.SortFields.Add2 _
Key:=Range("C1:C3880"), SortOn:=xlSortOnValues, Order:=xlAscending, _
DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("ExampleSupplier").AutoFilter.Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Range("A1").Select
ActiveWorkbook.Worksheets("ExampleSupplier").AutoFilter.Sort.SortFields.Clear
ActiveWorkbook.Worksheets("ExampleSupplier").AutoFilter.Sort.SortFields.Add2 _
Key:=Range("A1:A3880"), SortOn:=xlSortOnValues, Order:=xlAscending, _
DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("ExampleSupplier").AutoFilter.Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
ActiveSheet.Range("$A$1:$E$3880").AutoFilter Field:=4, Criteria1:="0"
Range("A1:E3880").Select
Range("A24").Activate
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorDark2
.TintAndShade = -9.99786370433668E-02
.PatternTintAndShade = 0
End With
ActiveSheet.Range("$A$1:$E$3880").AutoFilter Field:=4
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
Columns("D:D").Select
Selection.Delete Shift:=xlToLeft
Range("A1:D46").Select
Range("B7").Activate
Application.CutCopyMode = False
Application.CutCopyMode = False
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"ExampleSupplier!R1C1:R46C4", Version:=7).CreatePivotTable TableDestination _
:="ExampleSupplier!R3C6", TableName:="PivotTable3", DefaultVersion:=7
Sheets("ExampleSupplier").Select
Cells(3, 6).Select
With ActiveSheet.PivotTables("PivotTable3")
.ColumnGrand = True
.HasAutoFormat = True
.DisplayErrorString = False
.DisplayNullString = True
.EnableDrilldown = True
.ErrorString = ""
.MergeLabels = False
.NullString = ""
.PageFieldOrder = 2
.PageFieldWrapCount = 0
.PreserveFormatting = True
.RowGrand = True
.SaveData = True
.PrintTitles = False
.RepeatItemsOnEachPrintedPage = True
.TotalsAnnotation = False
.CompactRowIndent = 1
.InGridDropZones = False
.DisplayFieldCaptions = True
.DisplayMemberPropertyTooltips = False
.DisplayContextTooltips = True
.ShowDrillIndicators = True
.PrintDrillIndicators = False
.AllowMultipleFilters = False
.SortUsingCustomLists = True
.FieldListSortAscending = False
.ShowValuesRow = False
.CalculatedMembersInFilters = False
.RowAxisLayout xlCompactRow
End With
With ActiveSheet.PivotTables("PivotTable3").PivotCache
.RefreshOnFileOpen = False
.MissingItemsLimit = xlMissingItemsDefault
End With
ActiveSheet.PivotTables("PivotTable3").RepeatAllLabels xlRepeatLabels
With ActiveSheet.PivotTables("PivotTable3").PivotFields("SKU Name")
.Orientation = xlRowField
.Position = 1
End With
ActiveSheet.PivotTables("PivotTable3").AddDataField ActiveSheet.PivotTables( _
"PivotTable3").PivotFields("Order"), "Sum of Order", xlSum
End Sub
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
