Advertisement
desislava_topuzakova

Excel Automation - Regular Exam - Solutions

Sep 11th, 2024
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.27 KB | None | 0 0
  1. Excel Automation - Regular Exam - Solutions
  2. -------------------------------------------
  3. Task 01
  4. ------------------------------------------
  5. Sub Regular_Exam_Task1_Solution()
  6.  
  7. ' Regular_Exam_Task1_Solution Macro
  8. '
  9. Columns("K:K").Select
  10. Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
  11. Range("K1").Select
  12. ActiveCell.FormulaR1C1 = "Days"
  13. Range("K2").Select
  14. ActiveCell.FormulaR1C1 = "=DAYS(RC[-1],RC[-2])"
  15. Range("K2").Select
  16. Selection.AutoFill Destination:=Range("K2:K264")
  17. Range("K2:K264").Select
  18. Columns("K:K").Select
  19. Selection.NumberFormat = "General"
  20. Range("A1:O264").Select
  21. Range("J4").Activate
  22. With Selection
  23. .HorizontalAlignment = xlCenter
  24. .VerticalAlignment = xlCenter
  25. .ReadingOrder = xlContext
  26. End With
  27. Range("A1:O1").Select
  28. With Selection.Interior
  29. .Pattern = xlSolid
  30. .PatternColorIndex = xlAutomatic
  31. .Color = 5296274
  32. End With
  33. Rows("1:1").RowHeight = 40.5
  34. Selection.Font.Bold = True
  35. With Selection
  36. .HorizontalAlignment = xlGeneral
  37. .VerticalAlignment = xlCenter
  38. .ReadingOrder = xlContext
  39. End With
  40. With Selection.Font
  41. .Name = "Calibri"
  42. .Size = 14
  43. .ThemeColor = xlThemeColorLight1
  44. .ThemeFont = xlThemeFontMinor
  45. End With
  46. With Selection
  47. .HorizontalAlignment = xlCenter
  48. .VerticalAlignment = xlCenter
  49. .WrapText = True
  50. .ReadingOrder = xlContext
  51. End With
  52.  
  53. Range("A1:O264").Select
  54. Range("A1").Activate
  55. With Selection.Borders(xlEdgeLeft)
  56. .LineStyle = xlContinuous
  57. .Weight = xlThin
  58. End With
  59. With Selection.Borders(xlEdgeTop)
  60. .LineStyle = xlContinuous
  61. .Weight = xlThin
  62. End With
  63. With Selection.Borders(xlEdgeBottom)
  64. .LineStyle = xlContinuous
  65. .Weight = xlThin
  66. End With
  67. With Selection.Borders(xlEdgeRight)
  68. .LineStyle = xlContinuous
  69. .Weight = xlThin
  70. End With
  71. With Selection.Borders(xlInsideVertical)
  72. .LineStyle = xlContinuous
  73. .Weight = xlThin
  74. End With
  75. With Selection.Borders(xlInsideHorizontal)
  76. .LineStyle = xlContinuous
  77. .Weight = xlThin
  78. End With
  79. With Selection
  80. .VerticalAlignment = xlCenter
  81. .WrapText = True
  82. .ReadingOrder = xlContext
  83. End With
  84.  
  85. End Sub
  86. ------------------------------------
  87. Task 02
  88. ------------------------------------
  89. Sub Regular_Exam_Task2_Solution()
  90.  
  91. Dim CompanyName As String, Salary As Integer, Bonus As Integer
  92. CompanyName = Range("B1").Value
  93.  
  94. If CompanyName = "SoftUni" Then
  95. Salary = 2500
  96. Bonus = 1200
  97. ElseIf CompanyName = "Microsoft" Then
  98. Salary = 2700
  99. Bonus = 1400
  100. ElseIf CompanyName = "SAP" Or CompanyName = "sap" Then
  101. Salary = 3100
  102. Bonus = 1500
  103. End If
  104.  
  105.  
  106. Range("B2").Value = Salary
  107. Range("B3").Value = Bonus
  108.  
  109. End Sub
  110. -------------------------------
  111. Task 03
  112. -------------------------------
  113. Sub Regular_Exam_Task3_Solution()
  114.  
  115. Dim StartNumber As Integer, EndNumber As Integer, NumbersRange As String, Sum As Integer
  116.  
  117. StartNumber = Range("B1").Value
  118. EndNumber = Range("B2").Value
  119.  
  120. Dim Num As Integer
  121. For Num = StartNumber To EndNumber
  122. NumbersRange = NumbersRange & Num & " "
  123. Sum = Sum + Num
  124. Next Num
  125.  
  126. Range("B3").Value = Trim(NumbersRange)
  127. Range("B4").Value = Sum
  128.  
  129. End Sub
  130. ----------------------------------
  131. Task 04
  132. ----------------------------------
  133. Sub Exam_Preparation_Task4_Solution()
  134.  
  135. Dim InputText As String
  136. InputText = Range("A1").Value ' "1 2 3 4 5 6"
  137.  
  138. Dim Numbers As Variant
  139. Numbers = Split(InputText, " ") ' array of strings -> ["1", "2", "3", "4", "5", "6"]
  140. ' "1 2 3 4 5 6" -> Split " " -> ["1", "2", "3", "4", "5", "6"]
  141.  
  142. Dim TargetNumber As Integer
  143. TargetNumber = Range("A2").Value
  144.  
  145. Range("A3").Value = "Result:"
  146.  
  147. Dim Result As String
  148. Result = "" 'number which are bigger than TargetNumber
  149.  
  150. Dim Number As Variant ' every element in array
  151. For Each Number In Numbers
  152. 'Number = "1"
  153. If CInt(Number) < TargetNumber Then
  154. Result = Result & Number & " "
  155. End If
  156. Next
  157.  
  158. Range("B3").Value = Trim(Result)
  159.  
  160. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement