Advertisement
51L3N7

DeployLineUp

Sep 22nd, 2019
417
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ' [Embasa] Deploys the line-up of a work schedule
  2. ' @author Roger Pestana (e367982) — DG/GPEP
  3. ' @date 08/dec/2019
  4.  
  5. ' Returns a array length
  6. Function ArrayLength(ByVal arr As Variant) As Integer
  7.         ArrayLength = UBound(arr) - LBound(arr) + 1
  8. End Function
  9.  
  10. ' Returns the number of days passed between a year range
  11. Function DaysGoneByYear(ByVal year1 As Integer, ByVal year2 As Integer)
  12.         DaysGoneByYear = DateDiff("d", DateValue("01/01/" & year1), DateValue("01/01/" & year2))
  13.         If DaysGoneByYear < 0 Then DaysGoneByYear = -DaysGoneByYear
  14. End Function
  15.  
  16. ' Verifies if the date is a holiday
  17. Function IsHoliday(ByVal current_date As String, ByVal referenceYear As Integer) As Boolean
  18.         Dim found As Integer
  19.         found = False
  20.  
  21.         ' Fixed holidays
  22.        For i = 0 To ArrayLength(holidays) - 1
  23.                 If DateValue(holidays(i) & "/" & referenceYear) = current_date Then
  24.                         holidays_found_list(holiday_found_index) = Left(holidays(i), 2) & " - " & holidays_names(i)
  25.                         found = True
  26.                         Exit For
  27.                 End If
  28.         Next i
  29.  
  30.         ' Unfixed holidays
  31.        If referenceYear >= BASE_YEAR Then
  32.                 If DateValue(holidays_unfixed_list(0)) = current_date Then 'Carnival
  33.                        holidays_found_list(holiday_found_index) = Left(holidays_unfixed_list(0), 2) & " - " & holidays_names_unfixed(0)
  34.                         found = True
  35.                 ElseIf DateValue(holidays_unfixed_list(1)) = current_date Then 'Good day
  36.                        holidays_found_list(holiday_found_index) = Left(holidays_unfixed_list(1), 2) & " - " & holidays_names_unfixed(1)
  37.                         found = True
  38.                 ElseIf DateValue(holidays_unfixed_list(2)) = current_date Then 'Corpus Christi
  39.                        holidays_found_list(holiday_found_index) = Left(holidays_unfixed_list(2), 2) & " - " & holidays_names_unfixed(2)
  40.                         found = True
  41.                 End If
  42.         End If
  43.  
  44.         IsHoliday = found
  45. End Function
  46.  
  47. ' Returns the letter which the column which is represented
  48. ' Max range is 52 (AZ)
  49. Function GetColumnLetter(ByVal col_index As Integer) As String
  50.         Dim letter As String
  51.         indexer_base = IIf(col_index > 26, 26, 0)
  52.  
  53.         If indexer_base = 0 Then
  54.                 letter = Chr(64 + col_index)
  55.         Else
  56.                 letter = "A" & Chr(64 + (col_index - indexer_base))
  57.         End If
  58.  
  59.         GetColumnLetter = letter
  60. End Function
  61.  
  62. ' Prints a cells with a default format
  63. Sub Printc(ByVal cell_ref As Range, ByVal cell_value As Variant, ByVal cell_color As Long, _
  64.         Optional ByVal font_size As Integer, _
  65.         Optional ByVal custom_column_width As Double, _
  66.         Optional ByVal custom_row_height As Double, _
  67.         Optional ByVal value_color As Long, _
  68.         Optional ByVal horizontal_alignment As Integer, _
  69.         Optional ByVal border As Integer, _
  70.         Optional ByVal font_name As String, _
  71.         Optional ByVal border_color As Long _
  72. )
  73.         With cell_ref
  74.                 .Value = cell_value
  75.                 .ColumnWidth = IIf(custom_column_width, custom_column_width, column_width)
  76.                 .RowHeight = IIf(custom_row_height, custom_row_height, 15)
  77.                 .HorizontalAlignment = IIf(horizontal_alignment, horizontal_alignment, xlCenter)
  78.                 .VerticalAlignment = xlCenter
  79.                 .Interior.Color = cell_color
  80.                 .Borders.Color = IIf(border_color, border_color, 0)
  81.                 .Borders.LineStyle = IIf(border, border, xlContinuous)
  82.                 .Font.Bold = True
  83.                 .Font.Size = IIf(font_size, font_size, 11)
  84.                 .Font.Color = IIf(value_color, value_color, 0)
  85.                 .Font.Name = IIf(font_name <> "", font_name, "Calibri")
  86.         End With
  87. End Sub
  88.  
  89. ' Merges cell by index range
  90. Sub MergeCellsByIndex(ByVal col_a As Integer, ByVal row_a As Integer, ByVal col_b As Integer, ByVal row_b As Integer, _
  91.         Optional ByVal horizontal_alignment As Integer _
  92. )
  93.         With Range(GetColumnLetter(col_a) & CStr(row_a), GetColumnLetter(col_b) & CStr(row_b))
  94.                 .Merge
  95.                 .VerticalAlignment = xlCenter
  96.                 .HorizontalAlignment = IIf(horizontal_alignment, horizontal_alignment, xlCenter)
  97.         End With
  98. End Sub
  99.  
  100. ' Border thick by index range
  101. Sub BorderThickness(ByVal row_a As Integer, ByVal col_a As Integer, ByVal row_b As Integer, ByVal col_b As Integer)
  102.         With Range(GetColumnLetter(row_a) & CStr(col_a), GetColumnLetter(row_b) & CStr(col_b))
  103.                 .Borders(xlEdgeTop).Weight = xlMedium
  104.                 .Borders(xlEdgeLeft).Weight = xlMedium
  105.                 .Borders(xlEdgeRight).Weight = xlMedium
  106.                 .Borders(xlEdgeBottom).Weight = xlMedium
  107.         End With
  108. End Sub
  109.  
  110. 'Main function
  111. Sub deployLineup(ByVal combo_lineup As Variant, ByVal txt_lineupName, ByVal txt_classesInitFormat, ByVal txt_classesNumber As String, _
  112.         ByVal txt_columnSize As Double, ByVal txt_referenceYear As Integer, ByVal txt_baseYear _
  113. )
  114. Dim holiday_found As Boolean
  115. Dim classes_number, current_year, current_row, current_col, holidays_unfixed_baseIndex As Integer
  116. Dim current_date As String
  117. Dim lineup, current_line, classes_init_format, months_cols As Variant
  118.  
  119. ' Clear everything
  120. Range("A:AZ").Clear
  121. ActiveSheet.Pictures.Delete
  122.  
  123. ' Remove gridlines
  124. ActiveWindow.DisplayGridlines = False
  125.  
  126. ' Setup variables
  127. baseYear_classesInitFormat = IIf(txt_baseYear <> "", txt_baseYear, BASE_YEAR)
  128. classes_number = txt_classesNumber
  129. current_year = txt_referenceYear
  130. lineup = Split(txt_lineup, ",")
  131. classes_init_format = Split(txt_classesInitFormat, ",")
  132. column_width = txt_columnSize
  133. current_line = Array(0, 0, 0, 0, 0)
  134. months_cols = Array(2, 0, 0, 0, 0, 0)
  135.  
  136. ' Get the line-up of the combobox
  137. lineup = Split(combo_lineup.Value, ",")
  138.  
  139. ' holidays list
  140. ' Unfixed holidays just settled until 2019 to 2030, with 2019 as the base year
  141. holidays = Array("01/01", "21/04", "01/05", "24/06", "02/07", "07/09", "12/10", "02/11", "15/11", "25/12")
  142. holiday_carnival = Array("05/03/2019", "25/02/2020", "16/02/2021", "01/03/2022", "21/02/2023", "13/02/2024", "04/03/2025", "17/02/2026", "09/fev/2027", "29/02/2028", "13/02/2029", "05/03/2030")
  143. holiday_goodfriday = Array("19/04/2019", "10/04/2020", "02/04/2021", "15/04/2022", "07/04/2023", "29/04/2024", "18/04/2025", "03/04/2026", "26/03/2027", "14/04/2028", "30/03/2029", "19/04/2030")
  144. holiday_corpuschristi = Array("20/06/2019", "11/06/2020", "03/06/2021", "16/06/2022", "08/06/2023", "30/05/2024", "19/06/2025", "04/06/2026", "27/05/2027", "15/06/2028", "31/05/2029", "20/06/2030")
  145. holidays_unfixed_baseIndex = txt_referenceYear Mod BASE_YEAR
  146.  
  147. ' Holidays names
  148. holidays_names = Array( _
  149.         "Confraternização Universal", "Tiradentes", _
  150.         "Dia do Trabalho", "São João", _
  151.         "Independência da Bahia", "Independência do Brasil", _
  152.         "Nossa Senhora Aparecida", "Finados", _
  153.         "Proclamação da República", "Natal" _
  154. )
  155. holidays_names_unfixed = Array("Carnaval", "Sexta-feira Santa", "Corpus Christi")
  156.  
  157. ' Holidays found
  158. holidays_found_list = Array("", "", "")
  159.  
  160. ' Set up the unfixed holidays
  161. If holidays_unfixed_baseIndex <= (MAX_HOLIDAYS_YEAR - BASE_YEAR) Then
  162.         holidays_unfixed_list = Array(holiday_carnival(holidays_unfixed_baseIndex), holiday_goodfriday(holidays_unfixed_baseIndex), holiday_corpuschristi(holidays_unfixed_baseIndex))
  163. End If
  164.  
  165. ' Fills the fixed months columns
  166. For i = 1 To ArrayLength(months_cols) - 1
  167.         months_cols(i) = months_cols(i - 1) + classes_number + 3
  168. Next i
  169.  
  170. ' Sets the first column width
  171. Cells(1, 1).ColumnWidth = 1
  172.  
  173. ' Set the init format of classes
  174. For i = 0 To classes_number - 1
  175.         current_line(i) = (classes_init_format(i) + DaysGoneByYear(baseYear_classesInitFormat, txt_referenceYear)) Mod ArrayLength(lineup)
  176.         If current_line(i) = 0 Then current_line(i) = ArrayLength(lineup)
  177.         current_line(i) = current_line(i) - 1
  178. Next i
  179.  
  180. ' Header
  181. Printc _
  182.         cell_ref:=Cells(HEADER_ROW, 2), _
  183.         cell_value:=txt_lineupName & IIf(txt_lineupName <> "", " — " & current_year, ""), _
  184.         cell_color:=HEADER_BGCOLOR, _
  185.         font_size:=20, _
  186.         custom_row_height:=25, _
  187.         value_color:=HEADER_VALUECOLOR, _
  188.         border:=xlNone, _
  189.         font_name:="Bahnschrift SemiLight"
  190. MergeCellsByIndex 2, HEADER_ROW, (6 * classes_number) + 18, HEADER_ROW
  191.  
  192. ' Main loop
  193. For current_month = 1 To 12
  194.         ' Set the indexes
  195.        ROW_BASE_INDEX = IIf(current_month < 7, FIRST_SEMESTER_ROW, SECOND_SEMESTER_ROW)
  196.         COL_BASE_INDEX = IIf(current_month < 7, 1, 7)
  197.         current_col = months_cols(current_month - COL_BASE_INDEX)
  198.  
  199.         ' Reset holidays list
  200.        holidays_found_list = Array("", "", "")
  201.         holiday_found_index = 0
  202.  
  203.         ' Print month name
  204.        Printc _
  205.                 cell_ref:=Cells(ROW_BASE_INDEX, current_col), _
  206.                 cell_value:=UCase(MonthName(current_month, True)), _
  207.                 cell_color:=MONTHNAME_BGCOLOR, _
  208.                 font_size:=14, _
  209.                 value_color:=MONTHNAME_VALUECOLOR, _
  210.                 border:=xlNone
  211.         MergeCellsByIndex current_col, ROW_BASE_INDEX, current_col + 1, ROW_BASE_INDEX + 1
  212.  
  213.         ' Print "Turmas" besides the month name
  214.        Printc _
  215.                 cell_ref:=Cells(ROW_BASE_INDEX, current_col + 2), _
  216.                 cell_value:="Turmas", _
  217.                 cell_color:=CLASSES_HEADER_BGCOLOR, _
  218.                 font_size:=14, _
  219.                 value_color:=CLASSES_HEADER_VALUECOLOR, _
  220.                 border:=xlNone
  221.         MergeCellsByIndex current_col + 2, ROW_BASE_INDEX, current_col + classes_number + 1, ROW_BASE_INDEX
  222.  
  223.         ' Prints the classes letters
  224.        For i = 0 To classes_number - 1
  225.                 Printc _
  226.                         cell_ref:=Cells(ROW_BASE_INDEX + 1, current_col + i + 2), _
  227.                         cell_value:=Chr(65 + i), _
  228.                         cell_color:=CLASSES_LETTERS_BGCOLOR, _
  229.                         font_size:=13, _
  230.                         value_color:=CLASSES_LETTERS_VALUECOLOR, _
  231.                         border:=xlNone
  232.         Next i
  233.        
  234.         ' Thick the month header
  235.        BorderThickness current_col, ROW_BASE_INDEX, current_col + 1 + classes_number, ROW_BASE_INDEX + 1
  236.  
  237.         ' Deploys each table line
  238.        For current_day = 1 To 31
  239.                 current_date = Format(current_day & "/" & current_month & "/" & current_year, "dd/mm/yyyy")
  240.                 current_row = current_day + ROW_BASE_INDEX + 1
  241.                 holiday_found = False
  242.  
  243.                 ' If isn't a date, set a gray background
  244.                If Not IsDate(current_date) Then
  245.                         For i = 0 To (31 - current_day)
  246.                                 For j = 0 To (classes_number + 1)
  247.                                         Printc _
  248.                                                 cell_ref:=Cells(current_row + i, current_col + j), _
  249.                                                 cell_value:="", _
  250.                                                 cell_color:=EMPTY_BGCOLOR, _
  251.                                                 font_size:=11, _
  252.                                                 custom_column_width:=IIf(j < 2, DEFAULT_COLUMN_WIDTH, column_width)
  253.                                 Next j
  254.                         Next i
  255.                         Exit For
  256.                 End If
  257.  
  258.                 ' Restart line position
  259.                For i = 0 To ArrayLength(current_line) - 1
  260.                         If current_line(i) >= ArrayLength(lineup) Then current_line(i) = 0
  261.                 Next i
  262.  
  263.                 ' Verify if is a holiday
  264.                If IsHoliday(DateValue(current_date), current_year) Then
  265.                         holiday_found = True
  266.                         holiday_found_index = holiday_found_index + 1
  267.                 End If
  268.  
  269.                 ' Fills the weekdays
  270.                Printc _
  271.                         cell_ref:=Cells(current_row, current_col), _
  272.                         cell_value:=WeekdayName(Weekday(current_date), True), _
  273.                         cell_color:=WEEKDAY_BGCOLOR, _
  274.                         custom_column_width:=DEFAULT_COLUMN_WIDTH, _
  275.                         value_color:=WEEKDAY_VALUECOLOR, _
  276.                         border:=xlNone
  277.  
  278.                 ' Fills the days
  279.                Printc _
  280.                         cell_ref:=Cells(current_row, current_col + 1), _
  281.                         cell_value:=current_day, _
  282.                         cell_color:=IIf(holiday_found, RED, DAY_BGCOLOR), _
  283.                         custom_column_width:=DEFAULT_COLUMN_WIDTH, _
  284.                         value_color:=DAY_VALUECOLOR, _
  285.                         border:=xlNone
  286.  
  287.                 ' Fills the lineup
  288.                For i = 0 To classes_number - 1
  289.                         Printc _
  290.                                 cell_ref:=Cells(current_row, current_col + i + 2), _
  291.                                 cell_value:=IIf(lineup(current_line(i)) = 0, "", lineup(current_line(i))), _
  292.                                 cell_color:=IIf(lineup(current_line(i)) = 0, LINEUP_DAYOFF_BGCOLOR, LINEUP_BGCOLOR), _
  293.                                 custom_column_width:=column_width, _
  294.                                 value_color:=LINEUP_VALUECOLOR, _
  295.                                 font_name:="Arial", _
  296.                                 border_color:=BASIC_BORDERCOLOR
  297.                         current_line(i) = current_line(i) + 1
  298.                 Next i
  299.         Next current_day
  300.  
  301.         ' Thick the last deployed month borders
  302.        BorderThickness current_col, ROW_BASE_INDEX, current_col + classes_number + 1, ROW_BASE_INDEX + 32
  303.        
  304.         ' Set the width of column after the last deployed month
  305.        If current_month Mod 6 <> 0 Then
  306.                 Cells(1, current_col + classes_number + 2).ColumnWidth = 1
  307.         Else
  308.                 ' Prints "Gerência de Administração de Pessoal — GPEP" bellow the semesters and merge the entire scope of the semester
  309.                Printc _
  310.                         cell_ref:=Cells(ROW_BASE_INDEX + 37, 2), _
  311.                         cell_value:="Gerência de Administração de Pessoal — GPEP", _
  312.                         cell_color:=xlNone, _
  313.                         font_size:=14, _
  314.                         custom_column_width:=DEFAULT_COLUMN_WIDTH, _
  315.                         border:=xlNone
  316.                 MergeCellsByIndex 2, ROW_BASE_INDEX + 37, current_col + classes_number + 1, ROW_BASE_INDEX + 37
  317.                
  318.                 ' Prints "$1º semestre" above the semesters
  319.                Printc _
  320.                         cell_ref:=Cells(ROW_BASE_INDEX - 2, 2), _
  321.                         cell_value:=(current_month / 6) & "º semestre", _
  322.                         cell_color:=xlNone, _
  323.                         font_name:="Arial", _
  324.                         font_size:=15, _
  325.                         custom_column_width:=DEFAULT_COLUMN_WIDTH, _
  326.                         border:=xlNone
  327.                 MergeCellsByIndex 2, ROW_BASE_INDEX - 2, (6 * classes_number) + 18, ROW_BASE_INDEX - 2
  328.         End If
  329.  
  330.         ' Sets the first row height above the semester
  331.        Cells(ROW_BASE_INDEX + 33, current_col).RowHeight = 7
  332.  
  333.         ' Prints the holidays names above above each month
  334.        If holiday_found_index > 0 Then
  335.                 For i = 0 To ArrayLength(holidays_found_list) - 1
  336.                         If holidays_found_list(i) <> "" Then
  337.                                 Printc _
  338.                                         cell_ref:=Cells(ROW_BASE_INDEX + 34 + i, current_col), _
  339.                                         cell_value:=holidays_found_list(i), _
  340.                                         cell_color:=HOLIDAY_BGCOLOR, _
  341.                                         font_size:=13, _
  342.                                         custom_column_width:=DEFAULT_COLUMN_WIDTH, _
  343.                                         value_color:=HOLIDAY_VALUECOLOR, _
  344.                                         horizontal_alignment:=xlLeft, _
  345.                                         border:=xlNone
  346.                                 MergeCellsByIndex current_col, ROW_BASE_INDEX + 34 + i, current_col + classes_number + 1, ROW_BASE_INDEX + 34 + i, xlLeft
  347.                         End If
  348.                 Next i
  349.         End If
  350. Next current_month
  351.  
  352. ' Deploys the Embasa's logo
  353. ActiveSheet.Shapes.AddPicture _
  354.         Filename:=LOGO_FILEPATH, _
  355.         LinkToFile:=msoFalse, _
  356.         SaveWithDocument:=msoTrue, _
  357.         Left:=(Range("B1:" & GetColumnLetter((6 * classes_number) + 18) & "1").Width / 2) - (LOGO_WIDTH / 2), _
  358.         Top:=(Range("A1:A" & HEADER_ROW - 1).Height / 2) - (LOGO_HEIGHT / 2), _
  359.         Width:=LOGO_WIDTH, Height:=LOGO_HEIGHT
  360. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement