Visual Basic-Work Spreadsheet

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Module1 - 1

Sub Clean_Up_Report()

Dim SourceLastRow As Long


Dim SourceLastRow1 As Long
Dim SourceLastRow2 As Long
Dim SourceLastRow3 As Long
Dim SourceLastRow4 As Long

Dim OutputLastRow As Long

Dim SourceLastColumn As Long


Dim SourceLastColumn1 As Long
Dim SourceLastColumn2 As Long
Dim SourceLastColumn3 As Long
Dim SourceLastColumn4 As Long

Dim sourceSheet As Worksheet


Dim sourceSheet1 As Worksheet
Dim sourceSheet2 As Worksheet
Dim sourceSheet3 As Worksheet
Dim sourceSheet4 As Worksheet

Dim outputSheet As Worksheet

Set sourceSheet = Worksheets("iTrade Report")


Set sourceSheet1 = Worksheets("MSR")
Set sourceSheet2 = Worksheets("FromTo")
Set sourceSheet3 = Worksheets("Sysco OG")
Set sourceSheet4 = Worksheets("Sysco Stock Matrix")

Set outputSheet = Worksheets("Clean Up")

With sourceSheet
SourceLastRow = .Cells(.Rows.Count, "K").End(xlUp).Row
End With

With sourceSheet
SourceLastColumn = .Cells(.Columns.Count, "K").End(xlUp).Column
End With

With sourceSheet1
SourceLastRow1 = .Cells(.Rows.Count, "J").End(xlUp).Row
End With

With sourceSheet1
SourceLastColumn1 = .Cells(.Columns.Count, "J").End(xlUp).Column
End With

With sourceSheet2
SourceLastRow2 = .Cells(.Rows.Count, "A").End(xlUp).Row
End With

With sourceSheet2
SourceLastColumn2 = .Cells(.Columns.Count, "A").End(xlUp).Column
End With

With sourceSheet3
SourceLastRow3 = .Cells(.Rows.Count, "A").End(xlUp).Row
End With

With sourceSheet3
SourceLastColumn3 = .Cells(.Columns.Count, "A").End(xlUp).Column
End With

With sourceSheet4
SourceLastRow4 = .Cells(.Rows.Count, "A").End(xlUp).Row
End With

With sourceSheet4
SourceLastColumn4 = .Cells(.Columns.Count, "A").End(xlUp).Column
End With
Module1 - 2

'Copy Product Number, Product Name, Pack Size, and Description


LastRow = sourceSheet1.Cells(Rows.Count, 3).End(xlUp).Row
sourceSheet1.Range("E13:H13", "E" & LastRow).Copy
outputSheet.Range("F12").PasteSpecial xlPasteValues
outputSheet.Activate

'Copies DC Number, DC Name, and Decentralized National ID Number


LastRow = sourceSheet1.Cells(Rows.Count, 3).End(xlUp).Row
sourceSheet1.Range("A13:C13", "A" & LastRow).Copy
outputSheet.Range("A12:C12").PasteSpecial xlPasteValues
outputSheet.Activate

'DC Name on iTrade Report


With sourceSheet
SourceLastRow = .Cells(.Rows.Count, "M").End(xlUp).Row
.Range("O2:O" & SourceLastRow).Formula = _
"=SUBSTITUTE($M2,""SYSCO "","""")"
End With

'Define Number of months on MSR


With sourceSheet1
SourceLastRow1 = .Cells(.Rows.Count, "A").End(xlUp).Row
.Range("K2").Formula = _
"=SUMPRODUCT((D12:D100000 <> """")/COUNTIF(D12:D100000,D12:D100000 & """"))"
.Range("K2").Font.Color = vbWhite

End With

'Populate Sector on CleanUp Sheet


'Part I
With outputSheet
OutputLastRow = .Cells(Rows.Count, "A").End(xlUp).Row
.Range("AY12:AY" & OutputLastRow).Formula = _
"=IFERROR(INDEX($AZ$12:$AZ$17,AGGREGATE(15,6,MATCH(""*""&$AZ$12:$AZ$17&""*"",$C12,0)*(ROW($AZ$
12:$AZ$17)-ROW(AZ$12)+1),1)),"""")"
End With

'Part II
With outputSheet
OutputLastRow = .Cells(Rows.Count, "A").End(xlUp).Row
.Range("D12:D" & OutputLastRow).Formula = _
"=IF($AY12=$AZ$12,$BA$12,IF($AY12=$AZ$13,$BA$13,IF($AY12=$AZ$14,$BA$14,IF($AY12=$AZ$15,$BA$15,
IF($AY12=$AZ$16,$BA$16,IF($AY12=$AZ$17,$BA$17,""""))))))"
End With

'Fill in Order Guide Type


With outputSheet
OutputLastRow = .Cells(.Rows.Count, "F").End(xlUp).Row
.Range("E12:E" & OutputLastRow).Formula = _
"=IF($D12=$BA$12,$BB$12,IF($D12=$BA$13,$BB$13,IF($D12=$BA$14,$BB$14,IF($D12=$BA$15,$BB$15,IF($
D12=$BA$16,"""",IF($D12=$BA$17,$BB$17,""""))))))"
End With

'MONTHLY SUMMARIES

'Sum for Month of January


With outputSheet
OutputLastRow = .Cells(.Rows.Count, "F").End(xlUp).Row
.Range("J12:J" & OutputLastRow).Formula = _
"=SUMPRODUCT(('" & sourceSheet1.Name & "'!$I$13:$I$" & SourceLastRow1 & ")*('" & sourceShe
et1.Name & "'!D$13:D$" & SourceLastRow1 & "=$J$10)*('" & sourceSheet1.Name & "'!A$13:A$" & SourceLastR
ow1 & "=$A12)*('" & sourceSheet1.Name & "'!C$13:C$" & SourceLastRow1 & "=$C12)*('" & sourceSheet1.Name
& "'!E$13:E$" & SourceLastRow1 & "=$F12))"
End With
'Sum for Month of February
With outputSheet
Module1 - 3

OutputLastRow = .Cells(.Rows.Count, "F").End(xlUp).Row


.Range("K12:K" & OutputLastRow).Formula = _
"=SUMPRODUCT(('" & sourceSheet1.Name & "'!$I$13:$I$" & SourceLastRow1 & ")*('" & sourceShe
et1.Name & "'!D$13:D$" & SourceLastRow1 & "=$K$10)*('" & sourceSheet1.Name & "'!A$13:A$" & SourceLastR
ow1 & "=$A12)*('" & sourceSheet1.Name & "'!C$13:C$" & SourceLastRow1 & "=$C12)*('" & sourceSheet1.Name
& "'!E$13:E$" & SourceLastRow1 & "=$F12))"
End With
'Sum for Month of March
With outputSheet
OutputLastRow = .Cells(.Rows.Count, "F").End(xlUp).Row
.Range("L12:L" & OutputLastRow).Formula = _
"=SUMPRODUCT(('" & sourceSheet1.Name & "'!$I$13:$I$" & SourceLastRow1 & ")*('" & sourceShe
et1.Name & "'!D$13:D$" & SourceLastRow1 & "=$L$10)*('" & sourceSheet1.Name & "'!A$13:A$" & SourceLastR
ow1 & "=$A12)*('" & sourceSheet1.Name & "'!C$13:C$" & SourceLastRow1 & "=$C12)*('" & sourceSheet1.Name
& "'!E$13:E$" & SourceLastRow1 & "=$F12))"
End With
'Sum for Month of April
With outputSheet
OutputLastRow = .Cells(.Rows.Count, "F").End(xlUp).Row
.Range("M12:M" & OutputLastRow).Formula = _
"=SUMPRODUCT(('" & sourceSheet1.Name & "'!$I$13:$I$" & SourceLastRow1 & ")*('" & sourceShe
et1.Name & "'!D$13:D$" & SourceLastRow1 & "=$M$10)*('" & sourceSheet1.Name & "'!A$13:A$" & SourceLastR
ow1 & "=$A12)*('" & sourceSheet1.Name & "'!C$13:C$" & SourceLastRow1 & "=$C12)*('" & sourceSheet1.Name
& "'!E$13:E$" & SourceLastRow1 & "=$F12))"
End With
'Sum for Month of May
With outputSheet
OutputLastRow = .Cells(.Rows.Count, "F").End(xlUp).Row
.Range("N12:N" & OutputLastRow).Formula = _
"=SUMPRODUCT(('" & sourceSheet1.Name & "'!$I$13:$I$" & SourceLastRow1 & ")*('" & sourceShe
et1.Name & "'!D$13:D$" & SourceLastRow1 & "=$N$10)*('" & sourceSheet1.Name & "'!A$13:A$" & SourceLastR
ow1 & "=$A12)*('" & sourceSheet1.Name & "'!C$13:C$" & SourceLastRow1 & "=$C12)*('" & sourceSheet1.Name
& "'!E$13:E$" & SourceLastRow1 & "=$F12))"
End With
'Sum for Month of June
With outputSheet
OutputLastRow = .Cells(.Rows.Count, "F").End(xlUp).Row
.Range("O12:O" & OutputLastRow).Formula = _
"=SUMPRODUCT(('" & sourceSheet1.Name & "'!$I$13:$I$" & SourceLastRow1 & ")*('" & sourceShe
et1.Name & "'!D$13:D$" & SourceLastRow1 & "=$O$10)*('" & sourceSheet1.Name & "'!A$13:A$" & SourceLastR
ow1 & "=$A12)*('" & sourceSheet1.Name & "'!C$13:C$" & SourceLastRow1 & "=$C12)*('" & sourceSheet1.Name
& "'!E$13:E$" & SourceLastRow1 & "=$F12))"
End With
'Sum for Month of July
With outputSheet
OutputLastRow = .Cells(.Rows.Count, "F").End(xlUp).Row
.Range("P12:P" & OutputLastRow).Formula = _
"=SUMPRODUCT(('" & sourceSheet1.Name & "'!$I$13:$I$" & SourceLastRow1 & ")*('" & sourceShe
et1.Name & "'!D$13:D$" & SourceLastRow1 & "=$P$10)*('" & sourceSheet1.Name & "'!A$13:A$" & SourceLastR
ow1 & "=$A12)*('" & sourceSheet1.Name & "'!C$13:C$" & SourceLastRow1 & "=$C12)*('" & sourceSheet1.Name
& "'!E$13:E$" & SourceLastRow1 & "=$F12))"
End With
'Sum for Month of August
With outputSheet
OutputLastRow = .Cells(.Rows.Count, "F").End(xlUp).Row
.Range("Q12:Q" & OutputLastRow).Formula = _
"=SUMPRODUCT(('" & sourceSheet1.Name & "'!$I$13:$I$" & SourceLastRow1 & ")*('" & sourceShe
et1.Name & "'!D$13:D$" & SourceLastRow1 & "=$Q$10)*('" & sourceSheet1.Name & "'!A$13:A$" & SourceLastR
ow1 & "=$A12)*('" & sourceSheet1.Name & "'!C$13:C$" & SourceLastRow1 & "=$C12)*('" & sourceSheet1.Name
& "'!E$13:E$" & SourceLastRow1 & "=$F12))"
End With
'Sum for Month of September
With outputSheet
OutputLastRow = .Cells(.Rows.Count, "F").End(xlUp).Row
.Range("R12:R" & OutputLastRow).Formula = _
"=SUMPRODUCT(('" & sourceSheet1.Name & "'!$I$13:$I$" & SourceLastRow1 & ")*('" & sourceShe
et1.Name & "'!D$13:D$" & SourceLastRow1 & "=$R$10)*('" & sourceSheet1.Name & "'!A$13:A$" & SourceLastR
ow1 & "=$A12)*('" & sourceSheet1.Name & "'!C$13:C$" & SourceLastRow1 & "=$C12)*('" & sourceSheet1.Name
& "'!E$13:E$" & SourceLastRow1 & "=$F12))"
End With
'Sum for Month of October
Module1 - 4

With outputSheet
OutputLastRow = .Cells(.Rows.Count, "F").End(xlUp).Row
.Range("S12:S" & OutputLastRow).Formula = _
"=SUMPRODUCT(('" & sourceSheet1.Name & "'!$I$13:$I$" & SourceLastRow1 & ")*('" & sourceShe
et1.Name & "'!D$13:D$" & SourceLastRow1 & "=$S$10)*('" & sourceSheet1.Name & "'!A$13:A$" & SourceLastR
ow1 & "=$A12)*('" & sourceSheet1.Name & "'!C$13:C$" & SourceLastRow1 & "=$C12)*('" & sourceSheet1.Name
& "'!E$13:E$" & SourceLastRow1 & "=$F12))"
End With
'Sum for Month of November
With outputSheet
OutputLastRow = .Cells(.Rows.Count, "F").End(xlUp).Row
.Range("T12:T" & OutputLastRow).Formula = _
"=SUMPRODUCT(('" & sourceSheet1.Name & "'!$I$13:$I$" & SourceLastRow1 & ")*('" & sourceShe
et1.Name & "'!D$13:D$" & SourceLastRow1 & "=$T$10)*('" & sourceSheet1.Name & "'!A$13:A$" & SourceLastR
ow1 & "=$A12)*('" & sourceSheet1.Name & "'!C$13:C$" & SourceLastRow1 & "=$C12)*('" & sourceSheet1.Name
& "'!E$13:E$" & SourceLastRow1 & "=$F12))"
End With
'Sum for Month of December
With outputSheet
OutputLastRow = .Cells(.Rows.Count, "F").End(xlUp).Row
.Range("U12:U" & OutputLastRow).Formula = _
"=SUMPRODUCT(('" & sourceSheet1.Name & "'!$I$13:$I$" & SourceLastRow1 & ")*('" & sourceShe
et1.Name & "'!D$13:D$" & SourceLastRow1 & "=$U$10)*('" & sourceSheet1.Name & "'!A$13:A$" & SourceLastR
ow1 & "=$A12)*('" & sourceSheet1.Name & "'!C$13:C$" & SourceLastRow1 & "=$C12)*('" & sourceSheet1.Name
& "'!E$13:E$" & SourceLastRow1 & "=$F12))"
End With

'Eliminate Duplicate Rows


Dim MyRange As Range

LastRow = ActiveSheet.Range("F" & Rows.Count).End(xlUp).Row


Set MyRange = ActiveSheet.Range("A11:AE" & LastRow)
MyRange.RemoveDuplicates Columns:=Array(2, 3, 4, 6), Header:=xlYes
Range("D:D,J:X").EntireColumn.HorizontalAlignment = xlCenter

'Border Around Case Quantity Sum


With outputSheet
OutputLastRow = .Cells(.Rows.Count, "F").End(xlUp).Row
.Range("V12:V" & OutputLastRow).Borders(xlEdgeLeft).Weight = xlMedium
.Range("V12:V" & OutputLastRow).Borders(xlEdgeRight).Weight = xlMedium
End With

'Border Separating Old from New


With outputSheet
OutputLastRow = .Cells(.Rows.Count, "F").End(xlUp).Row
.Range("X12:X" & OutputLastRow).Borders(xlEdgeLeft).Weight = xlThick
End With

'New Product Information

'FORMAT "FROM TO" SHEET AS TEXT


Dim rng As Range: Set rng = sourceSheet2.Range("A1:H1000").CurrentRegion
Dim cCell As Range
For Each cCell In rng.Cells
If IsNumeric(cCell.Value) Then
cCell.Value = "'" & CStr(cCell.Value)
End If
Next cCell

'FromTo Product Number


With outputSheet
OutputLastRow = .Cells(.Rows.Count, "F").End(xlUp).Row
.Range("Z12:Z" & OutputLastRow).Formula = _
"=IFERROR(INDEX('" & sourceSheet2.Name & "'!F$3:F$" & SourceLastRow2 & ",MATCH(1,INDEX(($F
12='" & sourceSheet2.Name & "'!A$3:A$" & SourceLastRow2 & ")*($I12='" & sourceSheet2.Name & "'!D$3:D$"
& SourceLastRow2 & "),0,1),0),1),INDEX('" & sourceSheet2.Name & "'!F$3:F$" & SourceLastRow2 & ",MATCH
($F12,'" & sourceSheet2.Name & "'!F$3:F$" & SourceLastRow2 & ",0)))"
End With
Module1 - 5

'FromTo Product Description


With outputSheet
OutputLastRow = .Cells(.Rows.Count, "F").End(xlUp).Row
.Range("AA12:AA" & OutputLastRow).Formula = _
"=IFERROR(VLOOKUP(F12,'" & sourceSheet2.Name & "'!$A$3:$I$" & SourceLastRow2 & ",7,FALSE),
INDEX('" & sourceSheet2.Name & "'!$G$3:$G$" & SourceLastRow2 & ",MATCH($F12,'" & sourceSheet2.Name & "
'!$F$3:$F$" & SourceLastRow2 & ",0)))"
End With

'FromTo Brand ID
With outputSheet
OutputLastRow = .Cells(.Rows.Count, "F").End(xlUp).Row
.Range("AC12:AC" & OutputLastRow).Formula = _
"=VLOOKUP(Z12,'" & sourceSheet2.Name & "'!$F$3:$I$" & SourceLastRow2 & ",3,FALSE)"
End With

'Proprietary Status
With outputSheet
OutputLastRow = .Cells(.Rows.Count, "F").End(xlUp).Row
.Range("AE12:AE" & OutputLastRow).Formula = _
"=VLOOKUP(F12,'" & sourceSheet3.Name & "'!$C$2:$K$" & SourceLastRow3 & ",9,FALSE)"
End With

'Sector
With outputSheet
OutputLastRow = .Cells(.Rows.Count, "F").End(xlUp).Row
.Range("X12:X" & OutputLastRow).Formula = _
"=IF($D12="""","""",$D12)"
End With

'Order Guide
With outputSheet
OutputLastRow = .Cells(.Rows.Count, "F").End(xlUp).Row
.Range("Y12:Y" & OutputLastRow).Formula = _
"=IF($E12="""","""",$E12)"
End With

'Stocking Status
With outputSheet
OutputLastRow = .Cells(.Rows.Count, "F").End(xlUp).Row
.Range("AD12:AD" & OutputLastRow).Formula = _
"=INDEX('" & sourceSheet4.Name & "'!I$2:I$" & SourceLastRow4 & ",MATCH(1,INDEX((A12='" & sourc
eSheet4.Name & "'!A$2:A$" & SourceLastRow4 & ")*(Z12='" & sourceSheet4.Name & "'!B$2:B$" & SourceLastR
ow4 & "),0,1),0),1)"
End With

'Pack / Size
With outputSheet
OutputLastRow = .Cells(.Rows.Count, "F").End(xlUp).Row
.Range("AB12:AB" & OutputLastRow).Formula = _
"=IFERROR(VLOOKUP(Z12,'" & sourceSheet1.Name & "'!$E$3:$G$" & SourceLastRow1 & ",3,FALSE),""""
)"
End With

'Format Entire Worksheet as "General"


ActiveSheet.Cells.NumberFormat = "General"

'Calculation Formulas
'Sum Case Quantity
With outputSheet
OutputLastRow = .Cells(.Rows.Count, "F").End(xlUp).Row
.Range("V12:V" & OutputLastRow).Formula = _
"=IF(SUM($J12:$U12)="""","""",SUM($J12:$U12))"
End With

'Average Case Quantity


With outputSheet
OutputLastRow = .Cells(.Rows.Count, "F").End(xlUp).Row
Module1 - 6

.Range("W12:W" & OutputLastRow).Formula = _


"=IF($V12/'" & sourceSheet1.Name & "'!$K$2>1,$V12/'" & sourceSheet1.Name & "'!$K$2,""Less
Than 1"")"
.Range("W12:W" & OutputLastRow).NumberFormat = "####"
'IF($V12/'" & sourceSheet2.Name & "'!$K$2>1,$V12/'" & sourceSheet2.Name & "'!$K$2,"Less Th
an 1")
End With

End Sub

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy