Advertisement
desislava_topuzakova

Exam Preparation - Solutions

Sep 4th, 2024
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.88 KB | None | 0 0
  1. Exam Preparation - Solution
  2. -----------------------------
  3. Task 1 - Solution
  4. -----------------------------
  5. Sub Exam_Preparation_Task1_Solution()
  6. Range("F2").Select
  7. ActiveCell.FormulaR1C1 = "=IF(RC[-1]>=100000,RC[-1]*3%,RC[-1]*2%)"
  8. Range("F2").Select
  9. Selection.AutoFill Destination:=Range("F2:F99")
  10. Range("F2:F99").Select
  11. ActiveWindow.SmallScroll Down:=0
  12. Range("G2").Select
  13. Application.CutCopyMode = False
  14. ActiveCell.FormulaR1C1 = "=RC[-2]-RC[-1]"
  15. Range("G2").Select
  16. Selection.AutoFill Destination:=Range("G2:G99")
  17. Range("G2:G99").Select
  18. ActiveWindow.SmallScroll Down:=0
  19. Rows("1:1").Select
  20. Selection.RowHeight = 55
  21. Range("A1:G1").Select
  22. With Selection
  23. .HorizontalAlignment = xlCenter
  24. .VerticalAlignment = xlBottom
  25. .Orientation = 0
  26. .AddIndent = False
  27. .IndentLevel = 0
  28. .ShrinkToFit = False
  29. .ReadingOrder = xlContext
  30. .MergeCells = False
  31. End With
  32. With Selection
  33. .HorizontalAlignment = xlCenter
  34. .VerticalAlignment = xlCenter
  35. .Orientation = 0
  36. .AddIndent = False
  37. .IndentLevel = 0
  38. .ShrinkToFit = False
  39. .ReadingOrder = xlContext
  40. .MergeCells = False
  41. End With
  42. With Selection.Font
  43. .Name = "Calibri"
  44. .Size = 15
  45. .Strikethrough = False
  46. .Superscript = False
  47. .Subscript = False
  48. .OutlineFont = False
  49. .Shadow = False
  50. .Underline = xlUnderlineStyleNone
  51. .ThemeColor = xlThemeColorLight1
  52. .TintAndShade = 0
  53. .ThemeFont = xlThemeFontMinor
  54. End With
  55. Selection.Font.Bold = True
  56. With Selection
  57. .HorizontalAlignment = xlCenter
  58. .VerticalAlignment = xlCenter
  59. .WrapText = True
  60. .Orientation = 0
  61. .AddIndent = False
  62. .IndentLevel = 0
  63. .ShrinkToFit = False
  64. .ReadingOrder = xlContext
  65. .MergeCells = False
  66. End With
  67. With Selection.Interior
  68. .Pattern = xlSolid
  69. .PatternColorIndex = xlAutomatic
  70. .Color = 49407
  71. .TintAndShade = 0
  72. .PatternTintAndShade = 0
  73. End With
  74. Columns("A:G").Select
  75. With Selection
  76. .HorizontalAlignment = xlGeneral
  77. .Orientation = 0
  78. .AddIndent = False
  79. .IndentLevel = 0
  80. .ShrinkToFit = False
  81. .ReadingOrder = xlContext
  82. .MergeCells = False
  83. End With
  84. With Selection
  85. .HorizontalAlignment = xlCenter
  86. .Orientation = 0
  87. .AddIndent = False
  88. .IndentLevel = 0
  89. .ShrinkToFit = False
  90. .ReadingOrder = xlContext
  91. .MergeCells = False
  92. End With
  93. ActiveWindow.SmallScroll Down:=77
  94. Range("C92").Select
  95. ActiveWindow.SmallScroll Down:=-77
  96. Range("A1:G99").Select
  97. Selection.Borders(xlDiagonalDown).LineStyle = xlNone
  98. Selection.Borders(xlDiagonalUp).LineStyle = xlNone
  99. With Selection.Borders(xlEdgeLeft)
  100. .LineStyle = xlContinuous
  101. .ColorIndex = 0
  102. .TintAndShade = 0
  103. .Weight = xlThin
  104. End With
  105. With Selection.Borders(xlEdgeTop)
  106. .LineStyle = xlContinuous
  107. .ColorIndex = 0
  108. .TintAndShade = 0
  109. .Weight = xlThin
  110. End With
  111. With Selection.Borders(xlEdgeBottom)
  112. .LineStyle = xlContinuous
  113. .ColorIndex = 0
  114. .TintAndShade = 0
  115. .Weight = xlThin
  116. End With
  117. With Selection.Borders(xlEdgeRight)
  118. .LineStyle = xlContinuous
  119. .ColorIndex = 0
  120. .TintAndShade = 0
  121. .Weight = xlThin
  122. End With
  123. With Selection.Borders(xlInsideVertical)
  124. .LineStyle = xlContinuous
  125. .ColorIndex = 0
  126. .TintAndShade = 0
  127. .Weight = xlThin
  128. End With
  129. With Selection.Borders(xlInsideHorizontal)
  130. .LineStyle = xlContinuous
  131. .ColorIndex = 0
  132. .TintAndShade = 0
  133. .Weight = xlThin
  134. End With
  135. Range("A1").Select
  136. End Sub
  137. ---------------------------------
  138. Task 2 - Solution
  139. ---------------------------------
  140. Sub Exam_Preparation_Task2_Solution()
  141.  
  142. Dim ProductName As String
  143. ProductName = Range("B1").Value
  144.  
  145. Dim ProductQuantity As Double
  146. ProductQuantity = Range("B2").Value
  147.  
  148. Dim Town As String
  149. Town = Range("B3").Value
  150.  
  151.  
  152. ' TotalPrice = ProductQuantity * SinglePrice
  153.  
  154. 'SinglePrice -> Town, ProductName
  155. Dim SinglePrice As Double
  156. SinglePrice = 0
  157.  
  158.  
  159. If Town = "Sofia" Then
  160. ' check which is the product bought in Sofia
  161. Select Case ProductName
  162. Case "coffee"
  163. SinglePrice = 2.5
  164. Case "water"
  165. SinglePrice = 1.4
  166. Case "juice"
  167. SinglePrice = 3.4
  168. End Select
  169. ElseIf Town = "Varna" Then
  170. ' check which is the product bought in Varna
  171. Select Case ProductName
  172. Case "coffee"
  173. SinglePrice = 2.2
  174. Case "water"
  175. SinglePrice = 1.1
  176. Case "juice"
  177. SinglePrice = 3
  178. End Select
  179. End If
  180.  
  181. ' we know single price of the product
  182. Dim TotalPrice As Double
  183. TotalPrice = SinglePrice * ProductQuantity
  184.  
  185. Range("B4").Value = TotalPrice
  186.  
  187. End Sub
  188. ---------------------------------
  189. Task 3 - Solution
  190. ---------------------------------
  191. Sub Exam_Preparation_Task3_Solution()
  192.  
  193. Dim Row As Integer 'current row
  194. Dim LastRow As Integer 'last row in table
  195.  
  196. Dim SumEven, SumOdd As Integer
  197. SumEven = 0
  198. SumOdd = 0
  199.  
  200. LastRow = Cells(Rows.Count, 1).End(xlUp).Row
  201.  
  202. ' we have all numbers
  203. For Row = 2 To LastRow
  204. Dim Number As Integer 'current number
  205. Number = Range("A" & Row).Value
  206.  
  207. ' 1. write the type
  208. If Number Mod 2 = 0 Then
  209. ' number is even
  210. Range("B" & Row).Value = "even"
  211. SumEven = SumEven + Number
  212. Else
  213. ' number is odd
  214. Range("B" & Row).Value = "odd"
  215. SumOdd = SumOdd + Number
  216. End If
  217.  
  218. '2. write last digit
  219. Dim LastDigit As Integer
  220. LastDigit = Number Mod 10
  221. Range("C" & Row).Value = LastDigit
  222. Next Row
  223.  
  224. Range("F1").Value = SumEven
  225. Range("F2").Value = SumOdd
  226.  
  227. End Sub
  228. ---------------------------------
  229. Task 4 - Solution
  230. ---------------------------------
  231. Sub Exam_Preparation_Task4_Solution()
  232.  
  233. Dim InputText As String
  234. InputText = Range("A1").Value ' "1 2 3 4 5 6"
  235.  
  236. Dim Numbers As Variant
  237. Numbers = Split(InputText, " ") ' array of strings -> ["1", "2", "3", "4", "5", "6"]
  238. ' "1 2 3 4 5 6" -> Split " " -> ["1", "2", "3", "4", "5", "6"]
  239.  
  240. Dim TargetNumber As Integer
  241. TargetNumber = Range("A2").Value
  242.  
  243. Range("A3").Value = "Result:"
  244.  
  245. Dim Result As String
  246. Result = "" 'number which are bigger than TargetNumber
  247.  
  248. Dim Number As Variant ' every element in array
  249. For Each Number In Numbers
  250. 'Number = "1"
  251. If CInt(Number) > TargetNumber Then
  252. Result = Result & Number & " "
  253. End If
  254. Next
  255.  
  256. Range("B3").Value = Trim(Result)
  257.  
  258. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement