Sub D10_Common_Unique_NEW()
ActiveSheet.Shapes(Application.Caller).Name = "TheOneButt"
ActiveSheet.Shapes("TheOneButt").Select
Selection.Delete
ErrorNo = 999
ActiveSheet.Name = "Raw"
WB1 = Application.ActiveWorkbook.Name
Range("24:24").Select
If ActiveSheet.AutoFilterMode = True Then
Selection.AutoFilter
Else
End If
'To Exclude 2003 from the list
Selection.AutoFilter
Selection.AutoFilter Field:=2, Criteria1:="<>*2003*", Operator:=xlAnd
Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = "Raw 2"
Sheets("Raw").Select
Range("A:G").Select
Selection.Copy
Sheets("Raw 2").Select
Range("A1").Select
ActiveSheet.Paste
Range("1:23").Select
Selection.Delete Shift:=xlUp
Range("F1").FormulaR1C1 = ("Description")
Range("G1").FormulaR1C1 = ("Actual+Final")
Columns("A:A").Select
Selection.Cut
Columns("H:H").Select
Selection.Insert Shift:=xlToRight
Cells.Select
Cells.EntireColumn.AutoFit
Range("A61553").Select
Selection.End(xlUp).Select
ActiveCell.Offset(1, 0).Activate
ActiveCell.EntireRow.Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Delete Shift:=xlUp
'Replace Multiple location codes with a Name
Columns("A:A").Select
Selection.Replace What:="7710", Replacement:="Aus", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:="7711", Replacement:="Aus", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:="7712", Replacement:="Aus", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:="7713", Replacement:="Aus", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:="7714", Replacement:="Aus", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:="7715", Replacement:="Aus", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:="7716", Replacement:="Aus", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:="7801", Replacement:="Msia", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:="7803", Replacement:="Msia", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:="8601", Replacement:="HK", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:="8701", Replacement:="HK", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:="8801", Replacement:="JP", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:="8802", Replacement:="JP", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:="MHID", Replacement:="India", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:="MHIM", Replacement:="India", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
'Replace all Negative Figures with 1
For Each RowC In Range("F2:F23000").Rows
If RowC.Value = "" Then
GoTo Outside
ElseIf RowC.Value < 0 Then
RowC.Value = 1
End If
Next
Outside:
'Draw Pivot Table
Columns("A:G").Select
ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:= _
"'Raw 2'!A:G").CreatePivotTable TableDestination:="", TableName:= _
"PivotTable1", DefaultVersion:=xlPivotTableVersion10
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Product")
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Description")
.Orientation = xlRowField
.Position = 2
End With
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Location site SNP")
.Orientation = xlColumnField
.Position = 1
End With
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
"PivotTable1").PivotFields("Actual+Final"), "Count of Actual+Final", xlCount
Range("A1").Select
ActiveSheet.PivotTables("PivotTable1").PivotFields("Count of Actual+Final"). _
Function = xlSum
Range("A2").Select
ActiveSheet.PivotTables("PivotTable1").PivotFields("Product").Subtotals = Array _
(False, False, False, False, False, False, False, False, False, False, False, False)
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Location site SNP")
.PivotItems("(blank)").Visible = False
End With
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Product")
.PivotItems("(blank)").Visible = False
End With
ActiveSheet.Name = "PivotTable1"
Sheets("PivotTable1").Select
Sheets("PivotTable1").Move After:=Sheets(3)
Cells.Select
Cells.EntireColumn.AutoFit
Range("A61553").Select
Selection.End(xlUp).Select
ActiveCell.Offset(1, 0).Activate
ActiveCell.EntireRow.Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Delete Shift:=xlUp
'Create Working Sheet and Top15
Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = "Working"
Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = "For Top15"
Sheets("PivotTable1").Select
Range("A2").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Sheets("Working").Select
Range("A2").Select
ActiveSheet.Paste
'Top15 Tab
Sheets("For Top15").Select
Range("A2").Select
ActiveSheet.Paste
Cells.Select
Selection.Replace What:="0", Replacement:="", LookAt:=xlWhole, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Cells.EntireColumn.AutoFit
Range("AZ2").Select
Selection.End(xlToLeft).Select
ActiveCell.Offset(0, 1).Activate
ActiveCell.FormulaR1C1 = "Brand"
ColNo4Brand2 = ActiveCell.Column
Range("A2").Select
Selection.End(xlDown).Select
ActiveCell.EntireRow.Select
Selection.Delete Shift:=xlUp
Range("A2").Select
Selection.End(xlDown).Select
ActiveCell.Offset(0, ColNo4Brand2 - 1).Activate
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[" & (-ColNo4Brand2 + 1) & "],'Raw 2'!C[" & (-ColNo4Brand2 + 4) & "]:C[" & (-ColNo4Brand2 + 8) & "],4,0)"
ActiveCell.Copy
Range(Selection, Selection.End(xlUp)).Select
ActiveSheet.Paste
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "Brand"
ActiveCell.EntireColumn.Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Selection.Cut
Columns("C:C").Select
Selection.Insert Shift:=xlToRight
Rows("2:2").Select
Selection.AutoFilter
Range("A2").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
Range("A1").Select
ActiveCell.FormulaR1C1 = "File created on:"
Range("B1").Select
ActiveCell.FormulaR1C1 = "=NOW()"
Range("C1").Select
ActiveCell.FormulaR1C1 = "Units for ALL items are in SC"
Range("A1:C1").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'Converts EB to SC for champaigns
Top15LastRow = Range("AZ2").End(xlToLeft).End(xlDown).Row
For Each Top15EB In Range(Cells(3, 4), Cells(Top15LastRow, ColNo4Brand2)).Rows
'Nested Ifs
If Top15EB.Columns(1).Offset(0, -1).Value <> "CP" Then
If Top15EB.Columns(1).Offset(0, -1).Value <> "MC" Then
If Top15EB.Columns(1).Offset(0, -1).Value <> "DP" Then
If Top15EB.Columns(1).Offset(0, -1).Value <> "KG" Then
If Top15EB.Columns(1).Offset(0, -1).Value <> "RU" Then
'MsgBox "Not CP " & "Col" & Top15EB.Column & " row" & Top15EB.Row
GoTo Top15Loop
End If
End If
End If
End If
End If
For EB = 1 To ColNo4Brand2 - 4
If Top15EB.Columns(EB).Value = "" Then
GoTo NextEB
End If
ToSC = Top15EB.Columns(EB).Value
'MsgBox "Col" & Top15EB.Column & " row" & Top15EB.Row & " EB " & EB
ToSC = Round(ToSC * 75 / 900, 3)
Top15EB.Columns(EB).Value = ToSC
NextEB:
Next EB
Top15Loop:
Next
Range("B1").Select
With Selection
.HorizontalAlignment = xlLeft
.VerticalAlignment = xlBottom
End With
MsgBox "A prompt to Overwrite ""For Top15"" File will appear Next. Click Yes to save the file" & vbCrLf & vbCrLf & "If you click NO, please save the file manually"
Sheets("For Top15").Select
Sheets("For Top15").Move
On Error GoTo DoNotOverWr
ActiveWorkbook.SaveAs Filename:= _
"S:\Logistics\Outbound\Forecast meeting\Top15 items for all locations.xls", _
FileFormat:=xlNormal, Password:="", WriteResPassword:="", _
ReadOnlyRecommended:=False, CreateBackup:=False
If ErrorNo = 0 Then 'ErrorNo is 999 set on top
DoNotOverWr:
MsgBox "Please >Save< the file >Manually<"
End If
Workbooks(WB1).Activate
'Working Tab now
Sheets("Working").Select
Range("A2").Select
Selection.End(xlToRight).Select
ActiveCell.EntireColumn.Select
Selection.Delete Shift:=xlToLeft
Range("A2").Select
Selection.End(xlDown).Select
ActiveCell.EntireRow.Select
Selection.Delete Shift:=xlUp
'Remove all 0
Cells.Select
Selection.Replace What:="0", Replacement:="", LookAt:=xlWhole, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Cells.EntireColumn.AutoFit
'Replace all values above 0 to 1
FinalRow = Range("A65536").End(xlUp).Row
FinalCol = Range("AZ2").End(xlToLeft).Column
Range("B3").Select
For Each ColM In Worksheets("Working").Range("C3:AZ" & FinalRow).Columns
For Each RowM In Worksheets("Working").Range("C3:AZ" & FinalRow).Rows
ColNo = ColM.Column
If RowM.Columns(ColNo - 2).Value > 0 Then
RowM.Columns(ColNo - 2).Value = 1
End If
Next
Next
'Adding Grand Total in Working
Range("AZ2").Select
Selection.End(xlToLeft).Select
ActiveCell.Offset(0, 1).Activate
ActiveCell.FormulaR1C1 = "Grand Total"
ColNo4Total = ActiveCell.Column
Range("A2").Select
Selection.End(xlDown).Select
ActiveCell.Offset(0, ColNo4Total - 1).Activate
ActiveCell.FormulaR1C1 = "=Sum(RC[" & (-ColNo4Total + 3) & "] : RC[-1])"
ActiveCell.Copy
Range(Selection, Selection.End(xlUp)).Select
ActiveSheet.Paste
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "Grand Total"
'Adding Brand in Working
ActiveCell.Offset(0, 1).Activate
ActiveCell.FormulaR1C1 = "Brand"
ColNo4Brand = ActiveCell.Column
Range("A2").Select
Selection.End(xlDown).Select
ActiveCell.Offset(0, ColNo4Brand - 1).Activate
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[" & (-ColNo4Brand + 1) & "],'Raw 2'!C[" & (-ColNo4Brand + 4) & "]:C[" & (-ColNo4Brand + 8) & "],4,0)"
ActiveCell.Copy
Range(Selection, Selection.End(xlUp)).Select
ActiveSheet.Paste
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "Brand"
Set BrandLoc = ActiveCell '<< store "Brand" Location to sort later
ActiveCell.EntireColumn.Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'Draw Lines in Working
Range("A2").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
Rows("2:2").Select
Selection.AutoFilter
Range("C3").Select
Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = "Common SKUs"
Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = "Specific SKUs"
Sheets("Working").Select
'Sort by brand in ascending order
Selection.Sort Key1:=Range(BrandLoc.Address), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
'Seperate into Common and Unique Tabs
Selection.AutoFilter Field:=ColNo4Total, Criteria1:="1"
Cells.Select
Selection.Copy
Sheets("Specific SKUs").Select
Cells.Select
ActiveSheet.Paste
Cells.Select
Cells.EntireColumn.AutoFit
Range("C3").Select
Sheets("Working").Select
Selection.AutoFilter Field:=ColNo4Total, Criteria1:=">1", Operator:=xlAnd
Cells.Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Common SKUs").Select
Cells.Select
ActiveSheet.Paste
Cells.Select
Cells.EntireColumn.AutoFit
Columns("C:C").Select
Application.CutCopyMode = False
Selection.Insert Shift:=xlToRight
Range("C2").Select
ActiveCell.FormulaR1C1 = "Comments"
Range("C3").Select
Sheets("Working").Select
Selection.AutoFilter Field:=ColNo4Total
Sheets("Common SKUs").Select
End Sub