Advertisement
Robb1010

Waste

May 14th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Sub Waste()
  2. '
  3. ' Waste Macro
  4. '
  5. ' Keyboard Shortcut: Ctrl+Shift+W
  6. '
  7.  
  8.  
  9.  
  10. Dim wb As Workbook
  11. Set wb = ActiveWorkbook
  12. Dim ws As Worksheet
  13.  
  14. Application.DisplayAlerts = False
  15. 'Copy and paste to previous week waste
  16. Worksheets("Waste").Activate
  17. Cells.Select
  18.     Selection.Copy
  19.     Sheets("Previous week waste").Select
  20.     Cells.Select
  21.     ActiveSheet.Paste
  22.     Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
  23.         :=False, Transpose:=False
  24.  
  25.  
  26. 'Import the sames and waste reports as worksheets
  27. Set ws = wb.Sheets.Add(Type:="C:\Users\Sam4WinUser\Desktop\Robert\FV wastage\waste.Xls", After:=Application.ActiveSheet)
  28. ws.Name = "waste SAM"
  29.  
  30. Set ws = wb.Sheets.Add(Type:="C:\Users\Sam4WinUser\Desktop\Robert\FV wastage\sales.Xls", After:=Application.ActiveSheet)
  31. ws.Name = "sales SAM"
  32.  
  33. 'Delete empty columns in the SAM generated sales and waste reports
  34. Sheets("sales SAM").Range("B:B,D:D,F:F,H:I,K:K,N:N,Q:Q,S:S,V:V").EntireColumn.Delete
  35. Sheets("waste SAM").Range("B:D,F:I,K:K,M:P,S:S").EntireColumn.Delete
  36.  
  37. 'Copy the sales SAM worksheet to the sales sheet, then delete the sales SAM
  38. Worksheets("sales SAM").Activate
  39.  
  40. Range("C1").Select
  41. Selection.End(xlDown).Select
  42. Selection.Offset(2, 0).Select
  43. ActiveCell.Value = "sub total"
  44.  
  45. Cells.Select
  46. Selection.Copy
  47. Sheets("sales").Select
  48. Cells.Select
  49. ActiveSheet.Paste
  50.  
  51. Worksheets("sales SAM").Delete
  52. 'Done and dusted
  53.  
  54. 'Check and match the row numbers in the old and new waste work sheeets
  55. Worksheets("Waste").Activate
  56. Range("A3").Select
  57. Range(Selection, Selection.End(xlDown)).Select
  58. oldNumRows = Selection.Rows.Count
  59.  
  60. Worksheets("waste SAM").Activate
  61. Range("A2").Select
  62. Range(Selection, Selection.End(xlDown)).Select
  63. newNumRows = Selection.Rows.Count - 1
  64.  
  65. Worksheets("Waste").Activate
  66. If oldNumRows < newNumRows Then
  67.     difference = newNumRows - oldNumRows
  68.     Rows("28:" & difference + 27).Insert Shift:=xlDown, _
  69.       CopyOrigin:=xlFormatFromLeftOrBelow
  70.     End If
  71.  
  72. If oldNumRows > newNumRows Then
  73.     difference = oldNumRows - newNumRows
  74.     Rows("28:" & difference + 27).Delete
  75.     End If
  76. 'Matched
  77.  
  78. 'Copy and paste info from the waste SAM sheet to the Waste sheet and delete waste SAM sheet
  79. Worksheets("waste SAM").Activate
  80. Range("A2:F" & newNumRows + 1).Select
  81. Selection.Copy
  82. Worksheets("Waste").Activate
  83. Range("A3").PasteSpecial Paste:=xlPasteValues
  84.  
  85. Worksheets("waste SAM").Activate
  86. Range("G2:I" & newNumRows + 1).Select
  87. Selection.Copy
  88. Worksheets("Waste").Activate
  89. Range("I3").PasteSpecial Paste:=xlPasteValues
  90. 'Done and dusted :)
  91.  
  92. 'Reassign ranges
  93. Range("L3:O3").AutoFill Destination:=Range("L3:O" & newNumRows + 2)
  94.  
  95. Worksheets("waste SAM").Delete
  96.  
  97.  
  98. 'Sort things
  99. Range("F3").Select
  100.     Selection.Sort Key1:=Range("F2:F" & newNumRows + 1), Order1:=xlAscending, Header:=xlGuess, _
  101.         OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
  102.         DataOption1:=xlSortNormal
  103.        
  104. 'Assign order numbers
  105. Range("G3").Value = 1
  106. Range("G4").Value = 2
  107. Range("G3:G4").AutoFill Destination:=Range("G3:G" & newNumRows + 2)
  108.  
  109. 'Clear the comments area
  110. Range("P3:P" & newNumRows + 2).Select
  111.     Selection.ClearContents
  112.    
  113. 'Copy the group margin sales table
  114. Range("Q3:T20").Select
  115. Selection.Copy
  116. Worksheets("Sheet1").Activate
  117. Range("A4").PasteSpecial Paste:=xlPasteValues
  118. Worksheets("Waste").Activate
  119.    
  120. 'Check the group products
  121.  
  122.  
  123.  
  124. 'Fonts and borders
  125. With Selection.Font
  126.         .Name = "Calibri"
  127.         .Size = 10
  128.         .Strikethrough = False
  129.         .Superscript = False
  130.         .Subscript = False
  131.         .OutlineFont = False
  132.         .Shadow = False
  133.         .Underline = xlUnderlineStyleNone
  134.         .ColorIndex = 1
  135.         .TintAndShade = 0
  136.         .ThemeFont = xlThemeFontMinor
  137.     End With
  138.     Selection.Borders(xlDiagonalDown).LineStyle = xlNone
  139.     Selection.Borders(xlDiagonalUp).LineStyle = xlNone
  140.     Selection.Borders(xlEdgeLeft).LineStyle = xlNone
  141.     Selection.Borders(xlEdgeTop).LineStyle = xlNone
  142.     Selection.Borders(xlEdgeBottom).LineStyle = xlNone
  143.     Selection.Borders(xlEdgeRight).LineStyle = xlNone
  144.     Selection.Borders(xlInsideVertical).LineStyle = xlNone
  145.     Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
  146.     Selection.Borders(xlDiagonalDown).LineStyle = xlNone
  147.     Selection.Borders(xlDiagonalUp).LineStyle = xlNone
  148.     With Selection.Borders(xlEdgeLeft)
  149.         .LineStyle = xlContinuous
  150.         .ColorIndex = 0
  151.         .TintAndShade = 0
  152.         .Weight = xlThin
  153.     End With
  154.     With Selection.Borders(xlEdgeTop)
  155.         .LineStyle = xlContinuous
  156.         .ColorIndex = 0
  157.         .TintAndShade = 0
  158.         .Weight = xlThin
  159.     End With
  160.     With Selection.Borders(xlEdgeBottom)
  161.         .LineStyle = xlContinuous
  162.         .ColorIndex = 0
  163.         .TintAndShade = 0
  164.         .Weight = xlThin
  165.     End With
  166.     With Selection.Borders(xlEdgeRight)
  167.         .LineStyle = xlContinuous
  168.         .ColorIndex = 0
  169.         .TintAndShade = 0
  170.         .Weight = xlThin
  171.     End With
  172.     With Selection.Borders(xlInsideVertical)
  173.         .LineStyle = xlContinuous
  174.         .ColorIndex = 0
  175.         .TintAndShade = 0
  176.         .Weight = xlThin
  177.     End With
  178.     With Selection.Borders(xlInsideHorizontal)
  179.         .LineStyle = xlContinuous
  180.         .ColorIndex = 0
  181.         .TintAndShade = 0
  182.         .Weight = xlThin
  183.     End With
  184.     With Selection.Font
  185.         .Name = "Calibri"
  186.         .Size = 10
  187.         .Strikethrough = False
  188.         .Superscript = False
  189.         .Subscript = False
  190.         .OutlineFont = False
  191.         .Shadow = False
  192.         .Underline = xlUnderlineStyleNone
  193.         .ColorIndex = 1
  194.         .TintAndShade = 0
  195.         .ThemeFont = xlThemeFontMinor
  196.     End With
  197.    
  198.     Range("A3").Select
  199.     Range(Selection, Selection.End(xlDown)).Select
  200.     currentLastRow = Selection.Rows.Count + 2
  201.     Dim i As Integer
  202.     Worksheets("Waste").Activate
  203.     For i = 3 To currentLastRow
  204.         If InStr(Cells(i, 12).Text, "#N/A") Then
  205.             barcode = Cells(i, 3).Value
  206.             Worksheets("Sheet1").Activate
  207.             Set Rng = Range("G2:X23")
  208.             For Each cell In Rng
  209.                 If cell.Value = barcode Then
  210.                     cell.Select
  211.                     groupCell = Cells(1, Selection.Column).Value
  212.                     For j = 4 To 21
  213.                         If Cells(j, 1).Value = groupCell Then
  214.                             Worksheets("Sheet1").Range(Cells(j, 3), Cells(j, 4)).Copy
  215.                             Worksheets("Waste").Activate
  216.                             Range(Cells(i, 12), Cells(i, 13)).PasteSpecial Paste:=xlPasteFormats
  217.                             Range(Cells(i, 12), Cells(i, 13)).PasteSpecial Paste:=xlPasteFormulas
  218.                         End If
  219.                     Next j
  220.                 End If
  221.             Next cell
  222.         End If
  223.         Worksheets("Waste").Activate
  224. Next i
  225.      
  226. Range("N" & newNumRows + 2).Select
  227.  
  228. Application.DisplayAlerts = True
  229.    
  230. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement