Advertisement
desislava_topuzakova

Workshop - Solution

Aug 27th, 2024
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.55 KB | None | 0 0
  1. Sub Main()
  2. ' main procedure - all actionsover my table
  3. Copy_Data
  4. Insert_Columns
  5. Formatting_Protocol
  6. Fill_Practical_And_Theoretical_Points
  7. CoursePoints 80, 20
  8. Grade 70, 30
  9.  
  10. End Sub
  11.  
  12. Sub Copy_Data()
  13.  
  14. ' 1. Create new sheet
  15. ' 2. Copy username column
  16.  
  17. Dim Col As Variant
  18. Col = GetColumnsAddress("Data", "Username")
  19.  
  20. If Col = False Then
  21. MsgBox "Username not found"
  22. Else
  23. 'copy column
  24. Sheets("Data").Select
  25. Columns(Col).Select
  26. Selection.Copy
  27.  
  28. ' create new sheet
  29. Sheets.Add(Before:=Sheets("Data")).Name = "Protocol"
  30.  
  31. ' paste new sheet
  32. Sheets("Protocol").Select
  33. Columns(1).Select
  34. ActiveSheet.Paste
  35.  
  36. End If
  37.  
  38. End Sub
  39.  
  40. Function GetColumnsAddress(SheetName, TargetColumnName As String)
  41. Set Col = Sheets(SheetName).Rows(1).Find(What:=TargetColumnName) 'Set -> return whole object
  42.  
  43. 'maybe nothing
  44. If Col Is Nothing Then
  45. GetColumnsAddress = False
  46. Else
  47. GetColumnsAddress = Col.Column
  48. End If
  49. End Function
  50.  
  51. Sub Insert_Columns()
  52.  
  53. Sheets("Protocol").Select
  54. Range("B1") = "Common Points"
  55. Range("C1") = "Grade"
  56. Range("D1") = "Practical Points"
  57. Range("E1") = "Theoretical Points"
  58.  
  59. End Sub
  60.  
  61. Sub Formatting_Protocol()
  62. Sheets("Protocol").Select
  63. Range("A1").Select
  64. Selection.End(xlToRight).Select
  65. Range("A1").Select
  66. Range(Selection, Selection.End(xlToRight)).Select
  67. Range(Selection, Selection.End(xlDown)).Select
  68. ActiveWindow.SmallScroll Down:=-2690
  69. Application.CutCopyMode = False
  70. Selection.Borders(xlDiagonalDown).LineStyle = xlNone
  71. Selection.Borders(xlDiagonalUp).LineStyle = xlNone
  72. With Selection.Borders(xlEdgeLeft)
  73. .LineStyle = xlContinuous
  74. .ColorIndex = 0
  75. .TintAndShade = 0
  76. .Weight = xlThin
  77. End With
  78. With Selection.Borders(xlEdgeTop)
  79. .LineStyle = xlContinuous
  80. .ColorIndex = 0
  81. .TintAndShade = 0
  82. .Weight = xlThin
  83. End With
  84. With Selection.Borders(xlEdgeBottom)
  85. .LineStyle = xlContinuous
  86. .ColorIndex = 0
  87. .TintAndShade = 0
  88. .Weight = xlThin
  89. End With
  90. With Selection.Borders(xlEdgeRight)
  91. .LineStyle = xlContinuous
  92. .ColorIndex = 0
  93. .TintAndShade = 0
  94. .Weight = xlThin
  95. End With
  96. With Selection.Borders(xlInsideVertical)
  97. .LineStyle = xlContinuous
  98. .ColorIndex = 0
  99. .TintAndShade = 0
  100. .Weight = xlThin
  101. End With
  102. With Selection.Borders(xlInsideHorizontal)
  103. .LineStyle = xlContinuous
  104. .ColorIndex = 0
  105. .TintAndShade = 0
  106. .Weight = xlThin
  107. End With
  108. Selection.ColumnWidth = 30
  109. Range("A1:E1").Select
  110. With Selection
  111. .HorizontalAlignment = xlCenter
  112. .VerticalAlignment = xlBottom
  113. .WrapText = False
  114. .Orientation = 0
  115. .AddIndent = False
  116. .IndentLevel = 0
  117. .ShrinkToFit = False
  118. .ReadingOrder = xlContext
  119. .MergeCells = False
  120. End With
  121. With Selection.Font
  122. .Name = "Calibri"
  123. .Size = 14
  124. .Strikethrough = False
  125. .Superscript = False
  126. .Subscript = False
  127. .OutlineFont = False
  128. .Shadow = False
  129. .Underline = xlUnderlineStyleNone
  130. .ThemeColor = xlThemeColorLight1
  131. .TintAndShade = 0
  132. .ThemeFont = xlThemeFontMinor
  133. End With
  134. With Selection.Interior
  135. .Pattern = xlSolid
  136. .PatternColorIndex = xlAutomatic
  137. .ThemeColor = xlThemeColorDark1
  138. .TintAndShade = -0.249977111117893
  139. .PatternTintAndShade = 0
  140. End With
  141. Selection.Font.Bold = True
  142. End Sub
  143.  
  144. Sub Fill_Practical_And_Theoretical_Points()
  145. ' =VLOOKUP(A2, 'Practical Points'!A:B,2,FALSE)
  146. Sheets("Protocol").Select
  147.  
  148. 'Variant 1
  149. 'Range("D2").Formula = "=VLOOKUP(A2, 'Practical Points'!A:B,2,FALSE)"
  150. 'Range("D2").AutoFill Range("D2:D2711")
  151.  
  152. 'Range("E2").Formula = "=VLOOKUP(A2, 'Theoretical Points'!A:B,2,FALSE)"
  153. 'Range("E2").AutoFill Range("E2:E2711")
  154.  
  155. 'Variant 2
  156. Dim LastRow, Row As Integer
  157. LastRow = Cells(Rows.Count, 1).End(xlUp).Row
  158. For Row = 2 To LastRow
  159. Range("D" & Row) = "=VLOOKUP(A" & Row & ", 'Practical Points'!A:B,2,FALSE)"
  160. Range("E" & Row) = "=VLOOKUP(A" & Row & ", 'Theoretical Points'!A:B,2,FALSE)"
  161.  
  162. If Range("D" & Row) = 0 And Range("E" & Row) = 0 Then
  163. Range("D" & Row) = "NO"
  164. Range("E" & Row) = "NO"
  165. End If
  166. Next
  167.  
  168. End Sub
  169.  
  170. Sub CoursePoints(percentPractical, percentTheoretical As Integer)
  171. '=P*PP/ 100 + T*PT / 100
  172. '=D2*80/ 100 + E2*20 / 100
  173. 'P – Number of points from the Practical exam
  174. 'T – Number of points from the Theoretical exam
  175. 'PP – Percentage from the Practical exam
  176. 'PT – Percentage from the Theoretical exam
  177.  
  178. Sheets("Protocol").Select
  179. Dim LastRow, Row As Integer
  180. LastRow = Cells(Rows.Count, 1).End(xlUp).Row
  181.  
  182. Dim IsNotParticipate As Boolean
  183. IsNotParticipate = False
  184.  
  185. For Row = 2 To LastRow
  186. IsNotParticipate = Range("D" & Row) = "NO" And Range("E" & Row) = "NO"
  187.  
  188. If IsNotParticipate Then
  189. Range("B" & Row) = "Did not participate in the exam"
  190. Else
  191. Range("B" & Row) = _
  192. "=D" & Row & "*" & percentPractical & " / 100 + E" & Row & "*" & percentTheoretical & " / 100"
  193. End If
  194. Next
  195. End Sub
  196.  
  197. Sub Grade(pointsFor5, pointsFor3 As Integer)
  198.  
  199. Sheets("Protocol").Select
  200. '=IF(C=100,6,IF(C <=A,3+2*( C - B)/(A - B),5+( C - A)/(95 - A)))
  201. 'A – Minimum number of course points for a grade of 5
  202. 'B – Minimum number of course points for a grade of 3
  203. 'C – Number of course points (taken from the corresponding cell after calculation)
  204.  
  205. Dim LastRow, Row As Integer
  206. LastRow = Cells(Rows.Count, 1).End(xlUp).Row
  207.  
  208. For Row = 2 To LastRow
  209. If Range("B" & Row) = "Did not participate in the exam" Then
  210. Range("C" & Row) = ""
  211. ElseIf Range("B" & Row) = 100 Then
  212. Range("C" & Row) = 6
  213. ElseIf Range("B" & Row) <= pointsFor5 Then
  214. Range("C" & Row) = "=3 + 2 * (B" & Row & " - " & pointsFor3 & ")/(" & pointsFor5 & " - " & pointsFor3 & ")"
  215. If Range("C" & Row) < 2 Then
  216. Range("C" & Row) = 2
  217. End If
  218. Else
  219. Range("C" & Row) = "= 5 + (B" & Row & " - " & pointsFor5 & ")/ (95 - " & pointsFor5 & ")"
  220. If Range("C" & Row) > 6 Then
  221. Range("C" & Row) = 6
  222. End If
  223. End If
  224. Next
  225.  
  226. End Sub
  227.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement