Advertisement
veronikaaa86

Collections in VBA

Jan 10th, 2022
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.29 KB | None | 0 0
  1. Sub Example()
  2. Dim students As New Collection
  3.  
  4. students.Add ("Peter")
  5. students.Add ("Samantha")
  6. students.Remove (2)
  7.  
  8. Debug.Print students(1)
  9.  
  10. End Sub
  11. =============================================
  12. Sub Example()
  13. Dim students As New Collection
  14.  
  15. students.Add ("Peter")
  16. students.Add ("Samantha")
  17.  
  18. Dim count As Integer
  19. count = students.count
  20.  
  21. For i = 1 To count
  22. If students(i) = "Samantha" Then
  23. Debug.Print i
  24. End If
  25. Next
  26. End Sub
  27. =============================================
  28. Sub Example()
  29. Dim students As New Collection
  30.  
  31. students.Add ("Peter")
  32. students.Add ("Samantha")
  33.  
  34. For Each s In students
  35. Debug.Print s
  36. Next
  37. End Sub
  38. =============================================
  39. Sub Example()
  40. Dim numbers(0 To 5) As Integer
  41.  
  42. For i = 0 To UBound(numbers)
  43. numbers(i) = i + 10
  44. Next
  45. End Sub
  46. =============================================
  47. Sub Example()
  48. Dim numbers As Variant
  49.  
  50. numbers = Array(10, 20, 30, 40)
  51. End Sub
  52. =============================================
  53. Sub Example()
  54. Dim numbers As Variant
  55.  
  56. numbers = Array(10, 20, 30, 40)
  57. count = UBound(numbers)
  58.  
  59. ReDim numArr(0 To count + 1) As Integer
  60.  
  61. End Sub
  62. =============================================
  63. Sub Example()
  64. Dim days As Variant
  65.  
  66. days = Array( _
  67. "Monday", _
  68. "Tuesday", _
  69. "Wednesday", _
  70. "Thursday", _
  71. "Friday", _
  72. "Saturday", _
  73. "Sunday")
  74.  
  75. End Sub
  76. ==============================================
  77. Sub Example()
  78. Dim lastRow, row As Integer
  79. lastRow = Cells(Rows.Count, 2).End(xlUp).row
  80.  
  81. ReDim arr(0 To lastRow - 1)
  82.  
  83. For row = 1 To lastRow
  84. arr(row - 1) = Cells(row, 2)
  85. Next
  86. End Sub
  87. ==============================================
  88. Sub Example()
  89. Dim values As String
  90. values = Range("A1")
  91.  
  92. items = Split(values, " ")
  93.  
  94. For i = 0 To UBound(items)
  95. Debug.Print items(i)
  96. Next
  97.  
  98. End Sub
  99. ==============================================
  100. Sub Example()
  101. Dim arrFromRange, el As Variant
  102. arrFromRange = Range("C1:C6")
  103.  
  104. For Each el In arrFromRange
  105. Debug.Print el
  106. Next
  107. End Sub
  108. ==============================================
  109. mscorlib.dll
  110. ==============================================
  111. Dim names As New ArrayList
  112. names.Add("Peter")
  113. names.Add("Maria")
  114.  
  115. For Each name in names
  116. Debug.Print name
  117. Next
  118.  
  119. Debug.Print Join(names.toArray, ", ")
  120. ==============================================
  121. Sub Example()
  122. Dim nums As Variant
  123.  
  124. nums = Array(10, 20, 30)
  125.  
  126. For i = UBound(nums) To 0 Step -1
  127. Debug.Print nums(i)
  128. Next
  129.  
  130. End Sub
  131. ==============================================
  132. Sub Example()
  133. Dim arrOne As Variant
  134. arrOne = Array(10, 20, 30)
  135.  
  136. Dim arrTwo As Variant
  137. arrTwo = Array(10, 5, 30)
  138.  
  139. Dim count As Integer
  140. count = UBound(arrTwo)
  141.  
  142. Dim isIdentical As Boolean
  143. For i = 0 To count
  144. If arrOne(i) <> arrTwo(i) Then
  145. isIdentical = False
  146. Exit For
  147. Else
  148. isIdentical = True
  149. End If
  150. Next
  151.  
  152. If isIdentical Then
  153. Debug.Print "Yes"
  154. Else
  155. Debug.Print "No"
  156. End If
  157.  
  158. End Sub
  159. ==============================================
  160. Sub Example()
  161. Dim fruits As New Scripting.Dictionary
  162. ' Add to fruit to Dictionary
  163. fruits.Add key:="Apple", Item:=2.2
  164. fruits.Add key:="Peach", Item:=1.4
  165. fruits.Add key:="Plum", Item:=3.2
  166.  
  167. 'Debug.Print fruits("Apple")
  168.  
  169. Dim key As Variant
  170. For Each key In fruits.Keys
  171. Debug.Print key, fruits(key)
  172. Next key
  173. End Sub
  174. ==============================================
  175. Sub Example()
  176. Dim values As String
  177. values = "8 2.5 2.5 8 2.5"
  178.  
  179. Dim arrList As Variant
  180. arrList = Split(values, " ")
  181.  
  182. Dim dict As New Scripting.Dictionary
  183.  
  184. For Each num In arrList
  185. If dict.Exists(num) Then
  186.  
  187. dict(num) = dict(num) + 1
  188. Else
  189. dict.Add key:=num, Item:=1
  190. 'dict(num) = 1
  191. End If
  192. Next
  193.  
  194. Dim key As Variant
  195. For Each key In dict.Keys
  196. Debug.Print key, dict(key)
  197. Next key
  198.  
  199. End Sub
  200.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement