Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Sub breakdown()
- Dim i As Integer
- Dim counter As Integer
- Dim sumorder As Long
- Dim sumunits As Long
- Dim counterorders As Long
- Dim counterunits As Long
- Dim counteryear As Integer
- counteryear = 0
- Dim y As Integer
- Dim m As Integer
- Dim d As Integer
- Dim c As Integer
- Dim counter2 As Integer
- counter2 = 2
- Dim counter3 As Integer
- counter3 = 0
- c = 2
- Dim q As Integer
- q = 0
- 'Sort Data from Old to New in order for code to run properly'
- 'Sort Daily from Oldest to Current'
- ActiveWorkbook.Worksheets("Daily").AutoFilter.Sort.SortFields.Clear
- ActiveWorkbook.Worksheets("Daily").AutoFilter.Sort.SortFields.Add Key:=Range( _
- "B1:B3650"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
- xlSortNormal
- With ActiveWorkbook.Worksheets("Daily").AutoFilter.Sort
- .Header = xlYes
- .MatchCase = False
- .Orientation = xlTopToBottom
- .SortMethod = xlPinYin
- .Apply
- End With
- 'Sort Weekly from Oldest to Current'
- ActiveWorkbook.Worksheets("Weekly").AutoFilter.Sort.SortFields.Clear
- ActiveWorkbook.Worksheets("Weekly").AutoFilter.Sort.SortFields.Add Key:=Range _
- ("A1:A562"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
- xlSortNormal
- With ActiveWorkbook.Worksheets("Weekly").AutoFilter.Sort
- .Header = xlYes
- .MatchCase = False
- .Orientation = xlTopToBottom
- .SortMethod = xlPinYin
- .Apply
- End With
- 'Sort Monthly from Oldest to Current'
- ActiveWorkbook.Worksheets("Monthly").AutoFilter.Sort.SortFields.Clear
- ActiveWorkbook.Worksheets("Monthly").AutoFilter.Sort.SortFields.Add Key:= _
- Range("A1:A133"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption _
- :=xlSortNormal
- With ActiveWorkbook.Worksheets("Monthly").AutoFilter.Sort
- .Header = xlYes
- .MatchCase = False
- .Orientation = xlTopToBottom
- .SortMethod = xlPinYin
- .Apply
- End With
- 'Sort Quarterly from Oldest to Current'
- ActiveWorkbook.Worksheets("Quarterly").AutoFilter.Sort.SortFields.Clear
- ActiveWorkbook.Worksheets("Quarterly").AutoFilter.Sort.SortFields.Add Key:= _
- Range("D1:D48"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption _
- :=xlSortNormal
- With ActiveWorkbook.Worksheets("Quarterly").AutoFilter.Sort
- .Header = xlYes
- .MatchCase = False
- .Orientation = xlTopToBottom
- .SortMethod = xlPinYin
- .Apply
- End With
- ActiveWorkbook.Worksheets("Quarterly").AutoFilter.Sort.SortFields.Clear
- ActiveWorkbook.Worksheets("Quarterly").AutoFilter.Sort.SortFields.Add Key:= _
- Range("A1:A48"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
- xlSortNormal
- With ActiveWorkbook.Worksheets("Quarterly").AutoFilter.Sort
- .Header = xlYes
- .MatchCase = False
- .Orientation = xlTopToBottom
- .SortMethod = xlPinYin
- .Apply
- End With
- 'Sort Yearly from Oldest to Current'
- ActiveWorkbook.Worksheets("Yearly").AutoFilter.Sort.SortFields.Clear
- ActiveWorkbook.Worksheets("Yearly").AutoFilter.Sort.SortFields.Add Key:=Range _
- ("A1:A13"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
- xlSortNormal
- With ActiveWorkbook.Worksheets("Yearly").AutoFilter.Sort
- .Header = xlYes
- .MatchCase = False
- .Orientation = xlTopToBottom
- .SortMethod = xlPinYin
- .Apply
- End With
- 'Compile Data'
- 'Weekly Sheet!!'
- counter = 0
- For i = 4 To 3650
- 'Running Tally through the days'
- sumorder = sumorder + Sheets("Daily").Cells(i, 3)
- sumunits = sumunits + Sheets("Daily").Cells(i, 4)
- 'Checks the week as the running tally progresses. Every 7 days = output'
- If ((i - 3) Mod 7 = 0) Then
- Sheets("Weekly").Cells(counter + 3, 2) = sumorder
- Sheets("Weekly").Cells(counter + 3, 3) = sumunits
- counter = counter + 1
- sumorder = 0
- sumunits = 0
- End If
- Next i
- counter = counter + 1
- Sheets("Weekly").Cells(counter + 3, 2) = sumorder
- Sheets("Weekly").Cells(counter + 3, 3) = sumunits
- For r = 2 To 600
- 'Cleans out generated zeroes'
- If Sheets("Weekly").Cells(r, 2) = 0 Then
- Sheets("Weekly").Cells(r, 2) = ""
- Sheets("Weekly").Cells(r, 3) = ""
- End If
- Next r
- 'Monthly Sheet!!'
- For y = 2011 To 2021
- For m = 1 To 12
- counterorders = 0
- counterunits = 0
- 'Runs through data, one month at a time. Compiles it as monthly sums. Resets
- 'after each month'
- For d = 1 To 31
- If Sheets("Daily").Cells(c, 2) = (m + 3) & "/" & (d) & "/" & (y) Then
- counterorders = counterorders + Sheets("Daily").Cells(c, 3)
- counterunits = counterunits + Sheets("Daily").Cells(c, 4)
- c = c + 1
- End If
- Next d
- 'Output the compilation. Reset counter'
- countermonth = countermonth + 1
- Sheets("Monthly").Cells(countermonth + 1 + counteryear, 2) = counterorders
- Sheets("Monthly").Cells(countermonth + 1 + counteryear, 3) = counterunits
- Next m
- counterorders = 0
- counterunits = 0
- Next y
- counteryear = 12 * counteryear
- 'Clear out generated zeroes'
- For i = 2 To 150
- If Sheets("Monthly").Cells(i, 2) = 0 Then
- Sheets("Monthly").Cells(i, 2) = ""
- Sheets("Monthly").Cells(i, 3) = ""
- End If
- Next i
- counterorders = 0
- counterunits = 0
- y = 0
- 'Quarterly Sheet!!'
- For y = 1 To 40
- For q = 1 To 3
- counterorders = counterorders + Sheets("Monthly").Cells(q + 1 + counter3, 2)
- counterunits = counterunits + Sheets("Monthly").Cells(q + 1 + counter3, 3)
- 'Counter 3 shifts by 3 by reason 3 months per qtr. Counter 2 is the row shifter for output'
- Next q
- Sheets("Quarterly").Cells(counter2, 2) = counterorders
- Sheets("Quarterly").Cells(counter2, 3) = counterunits
- counter2 = counter2 + 1
- counterorders = 0
- counterunits = 0
- counter3 = counter3 + 3
- Next y
- 'Clearing out generated zeroes'
- For i = 2 To 45
- If Sheets("Quarterly").Cells(i, 2) = 0 Then
- Sheets("Quarterly").Cells(i, 2) = ""
- Sheets("Quarterly").Cells(i, 3) = ""
- End If
- Next i
- c = 2
- 'Yearly Sheet!!'
- For y = 2011 To 2021
- counterunits = 0
- counterorders = 0
- counteryear = counteryear + 1
- For m = 1 To 12
- 'Runs through the days in a year, and if there is data for a given date within a year,
- 'it adds it to the tally
- For d = 1 To 31
- If Sheets("Daily").Cells(c, 2) = (m + 3) & "/" & (d) & "/" & (y) Then
- counterorders = counterorders + Sheets("Daily").Cells(c, 3)
- counterunits = counterunits + Sheets("Daily").Cells(c, 4)
- c = c + 1
- End If
- Next d
- Next m
- 'Outputs yearly data, shifts row(countermonth) to next year'
- countermonth = countermonth + 1
- Sheets("Yearly").Cells(counteryear + 1, 2) = counterorders
- Sheets("Yearly").Cells(counteryear + 1, 3) = counterunits
- Next y
- 'Clear out generated zeroes'
- For i = 2 To 13
- If Sheets("Yearly").Cells(i, 2) = 0 Then
- Sheets("Yearly").Cells(i, 2) = ""
- Sheets("Yearly").Cells(i, 3) = ""
- End If
- Next i
- 'Sort Data from Current to Old for Display'
- 'Sort Daily from Current to Old'
- ActiveWorkbook.Worksheets("Daily").AutoFilter.Sort.SortFields.Clear
- ActiveWorkbook.Worksheets("Daily").AutoFilter.Sort.SortFields.Add Key:=Range( _
- "B1:B3650"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
- xlSortNormal
- With ActiveWorkbook.Worksheets("Daily").AutoFilter.Sort
- .Header = xlYes
- .MatchCase = False
- .Orientation = xlTopToBottom
- .SortMethod = xlPinYin
- .Apply
- End With
- 'Sort Weekly from Current to Old'
- ActiveWorkbook.Worksheets("Weekly").AutoFilter.Sort.SortFields.Clear
- ActiveWorkbook.Worksheets("Weekly").AutoFilter.Sort.SortFields.Add Key:=Range _
- ("A1:A562"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
- xlSortNormal
- With ActiveWorkbook.Worksheets("Weekly").AutoFilter.Sort
- .Header = xlYes
- .MatchCase = False
- .Orientation = xlTopToBottom
- .SortMethod = xlPinYin
- .Apply
- End With
- 'Sort Monthly from Current to Old'
- ActiveWorkbook.Worksheets("Monthly").AutoFilter.Sort.SortFields.Clear
- ActiveWorkbook.Worksheets("Monthly").AutoFilter.Sort.SortFields.Add Key:= _
- Range("A1:A133"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption _
- :=xlSortNormal
- With ActiveWorkbook.Worksheets("Monthly").AutoFilter.Sort
- .Header = xlYes
- .MatchCase = False
- .Orientation = xlTopToBottom
- .SortMethod = xlPinYin
- .Apply
- End With
- 'Sort Quarterly from Current to Old'
- ActiveWorkbook.Worksheets("Quarterly").AutoFilter.Sort.SortFields.Clear
- ActiveWorkbook.Worksheets("Quarterly").AutoFilter.Sort.SortFields.Add Key:= _
- Range("D1:D48"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption _
- :=xlSortNormal
- With ActiveWorkbook.Worksheets("Quarterly").AutoFilter.Sort
- .Header = xlYes
- .MatchCase = False
- .Orientation = xlTopToBottom
- .SortMethod = xlPinYin
- .Apply
- End With
- ActiveWorkbook.Worksheets("Quarterly").AutoFilter.Sort.SortFields.Clear
- ActiveWorkbook.Worksheets("Quarterly").AutoFilter.Sort.SortFields.Add Key:= _
- Range("A1:A48"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
- xlSortNormal
- With ActiveWorkbook.Worksheets("Quarterly").AutoFilter.Sort
- .Header = xlYes
- .MatchCase = False
- .Orientation = xlTopToBottom
- .SortMethod = xlPinYin
- .Apply
- End With
- 'Sort Yearly from Current to Old'
- ActiveWorkbook.Worksheets("Yearly").AutoFilter.Sort.SortFields.Clear
- ActiveWorkbook.Worksheets("Yearly").AutoFilter.Sort.SortFields.Add Key:=Range _
- ("A1:A13"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
- xlSortNormal
- With ActiveWorkbook.Worksheets("Yearly").AutoFilter.Sort
- .Header = xlYes
- .MatchCase = False
- .Orientation = xlTopToBottom
- .SortMethod = xlPinYin
- .Apply
- End With
- End Sub
Add Comment
Please, Sign In to add comment