51L3N7

Macro (Relação SSP/Comércio)

Apr 28th, 2016
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 7.12 KB | None | 0 0
  1. ' Deploys monthly list
  2. ' @author Roger Lima (rplima01)
  3. ' @date oct/2015
  4. ' @update 20/jul/2016
  5.  
  6. ' Verifies if the day is a holyday
  7. Function is_holyday(ByVal holydays_fixed As Variant, ByVal holydays_carnival As Variant, ByVal current_date As String) As Boolean
  8.         holyday_found = False
  9.        
  10.         ' Search in the list of fixed holydays
  11.         For Each holyday In holydays_fixed
  12.                 If DateValue(holyday) = DateValue(current_date) Then
  13.                         holyday_found = True
  14.                         Exit For
  15.                 End If
  16.         Next holyday
  17.        
  18.         ' Search in the carnival holyday list
  19.         For Each holyday In holydays_carnival
  20.                 end_holyday = DateValue(holyday) + 1
  21.                 holyday = DateValue(holyday) - 4
  22.                
  23.                 Do While DateValue(holyday) <> end_holyday + 1
  24.                         If DateValue(holyday) = DateValue(current_date) Then
  25.                                 holyday_found = True
  26.                                 Exit For
  27.                         End If
  28.                        
  29.                         holyday = DateValue(holyday) + 1
  30.                 Loop
  31.         Next holyday
  32.        
  33.         is_holyday = holyday_found
  34. End Function
  35.  
  36. Sub gerarRelação()
  37.  
  38. ' Holydays list
  39. ' Unfixed holydays just settled until 2026
  40. holydays = Array("01/01", "21/04", "01/05", "24/06", "02/07", "07/09", "12/10", "02/11", "15/11", "08/12", "24/12", "25/12", "31/12", "14/04/2017", "30/03/2018", "19/04/2019", "10/04/2020", "02/04/2021", "15/04/2022", "07/04/2023", "29/03/2024", "18/04/2025", "03/04/2026")
  41. holydays_carnival = Array("28/02/2017", "13/02/2018", "05/03/2019", "25/02/2020", "16/02/2021", "01/03/2022", "21/02/2023", "13/02/2024", "04/03/2025", "17/02/2026")
  42.  
  43. ' Date setup
  44. current_month = Month(DateValue("01 " & Split(ThisWorkbook.Name, ".")(0)))
  45. current_year = StrReverse(Split(StrReverse(ThisWorkbook.Path), "\")(0))
  46. sufix_date = "/" & current_month & "/" & current_year
  47.  
  48. ' Initialize day
  49. day_index = 1
  50.  
  51. ' Gets number of valid days in the month
  52. Do While True
  53.         current_date = day_index & sufix_date
  54.        
  55.         If Not IsDate(current_date) Then Exit Do
  56.        
  57.         ' Skips saturday, sunday and holydays
  58.         If Weekday(current_date) <> 1 And Weekday(current_date) <> 7 Then
  59.                 If Not is_holyday(holydays, holydays_carnival, current_date) Then valid_days = valid_days + 1
  60.         End If
  61.        
  62.         day_index = day_index + 1
  63. Loop
  64.  
  65. ' Adds worksheets missing
  66. Do While Worksheets.Count - 1 < valid_days
  67.         Worksheets(1).Copy Before:=Worksheets(Worksheets.Count)
  68. Loop
  69.  
  70. ' Removes worksheets excess
  71. Application.DisplayAlerts = False
  72. Do While Worksheets.Count - 1 > valid_days
  73.         Worksheets(1).Delete
  74. Loop
  75. Application.DisplayAlerts = True
  76.  
  77. day_index = 1
  78.  
  79. For Each ws In ThisWorkbook.Worksheets
  80.         ws.Activate
  81.        
  82.         If ws.Name <> "Estatística" Then
  83.                 current_date = day_index & sufix_date
  84.                
  85.                 ' Skips saturday, sunday and holydays
  86.                 Do While Weekday(current_date) = 1 Or Weekday(current_date) = 7 Or is_holyday(holydays, holydays_carnival, current_date)
  87.                         day_index = day_index + 1
  88.                         current_date = day_index & sufix_date
  89.                 Loop
  90.  
  91.                 For i = 8 To 308
  92.                         For j = 1 To IIf(i >= 8 And i <= 14, 8, 5)
  93.                                 If i = 8 And j < 7 Then j = 7
  94.                                 If j = 6 Then j = j + 1
  95.                                
  96.                                 ' Sets borders, alignments, font style and erases or fills cells
  97.                                 With Cells(i, j)
  98.                                         .Borders.LineStyle = xlContinuous
  99.                                         .VerticalAlignment = xlCenter
  100.                                         .Font.Name = "Calibri"
  101.                                         .Font.Size = 11
  102.                                         .Font.Italic = False
  103.                                         .Font.Underline = False
  104.                                         .Font.Bold = False
  105.                                         .Font.Color = RGB(0, 0, 0)
  106.                                        
  107.                                         If j = 1 Then
  108.                                                 .Value = i - 8
  109.                                                 .Font.Bold = True
  110.                                         Else
  111.                                                 .Value = ""
  112.                                         End If
  113.                                        
  114.                                         If j <> 2 Then
  115.                                                 .HorizontalAlignment = xlCenter
  116.                                         End If
  117.                                        
  118.                                         If j = 5 Then
  119.                                                 .Font.Bold = True
  120.                                         End If
  121.                                        
  122.                                         If i = 8 And j >= 7 Then
  123.                                                 .Font.Bold = True
  124.                                                 .Font.Name = "Arial"
  125.                                         End If
  126.                                 End With
  127.                         Next j
  128.                 Next i
  129.                
  130.                 ' Columns width
  131.                 Columns("A").ColumnWidth = 3.5
  132.                 Columns("B").ColumnWidth = 50
  133.                 Columns("C").ColumnWidth = 20
  134.                 Columns("D").ColumnWidth = 8.5
  135.                 Columns("E").ColumnWidth = 8.5
  136.                 Columns("G").ColumnWidth = 12
  137.                 Columns("H").ColumnWidth = 8
  138.                
  139.                 ' Daily statistic
  140.                 Range("G8").Value = "Estatística diária"
  141.                 Range("G8:H8").Merge
  142.                 Range("G9").Value = "1ª via"
  143.                 Range("G10").Value = "2ª via"
  144.                 Range("G11").Value = "3ª ou mais"
  145.                 Range("G12").Value = "MEC"
  146.                 Range("G13").Value = "RET. JUD"
  147.                 Range("G14").Value = "Total"
  148.                 Range("H9").Formula = "=COUNTIF(D9:D308,""1"")"
  149.                 Range("H10").Formula = "=COUNTIF(D9:D308,""2"")"
  150.                 Range("H11").Formula = "=COUNTIF(D9:D308,""3"")"
  151.                 Range("H12").Formula = "=COUNTIF(D9:D308,""MEC"")"
  152.                 Range("H13").Formula = "=COUNTIF(D9:D308,""RET.JUD"")+COUNTIF(D9:D308,""RET JUD"")+COUNTIF(D9:D308,""RET"")+COUNTIF(D9:D308,""RJ"")"
  153.                 Range("H14").Formula = "=SUM(H9:H13)"
  154.                
  155.                 ' Change date and worksheet name
  156.                 ws.Name = day_index & " " & UCase(MonthName(current_month, True))
  157.                 Range("C5").Value = current_month & "/" & Split(ws.Name)(0) & "/" & current_year
  158.                
  159.                 ' Deletes all below A308~E308 range
  160.                 Range("A309:E400").Delete
  161.  
  162.                 day_index = day_index + 1
  163.         End If
  164. Next
  165.  
  166. Call gerarEstatística
  167. End Sub
Advertisement
Add Comment
Please, Sign In to add comment