veronikaaa86

Loops VBA

Jan 6th, 2022
333
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.60 KB | None | 0 0
  1. 1*****************************************************
  2. Sub Example()
  3. For i = 1 To 100
  4. Debug.Print i
  5. Next i
  6.  
  7. End Sub
  8.  
  9. 2*****************************************************
  10.  
  11. Sub Example()
  12. Dim text As String
  13. text = Range("A1")
  14. Dim length, sum As Integer
  15. length = Len(text)
  16.  
  17. For i = 1 To length
  18. Dim letter As String
  19. letter = Mid(text, i, 1)
  20.  
  21. Debug.Print letter
  22. Next i
  23.  
  24. End Sub
  25. 3*****************************************************
  26.  
  27. Sub Example()
  28. Dim text As String
  29. text = Range("A1")
  30. Dim length, sum As Integer
  31. length = Len(text)
  32.  
  33. For num = 1 To length
  34. Dim letter As String
  35. letter = Mid(text, num, 1)
  36. Select Case letter
  37. Case "a"
  38. sum = sum + 1
  39. Case "e"
  40. sum = sum + 2
  41. Case "i"
  42. sum = sum + 3
  43. Case "o"
  44. sum = sum + 4
  45. Case "u"
  46. sum = sum + 5
  47.  
  48. End Select
  49.  
  50. Debug.Print letter
  51. Next num
  52.  
  53. Debug.Print sum
  54. End Sub
  55. 4*****************************************************
  56.  
  57. Sub Example()
  58. Dim n As Integer
  59. n = Range("A1")
  60.  
  61. Dim sum As Double
  62.  
  63. For i = 1 To n
  64. Dim number As Double
  65. number = Range("B" & i)
  66.  
  67. sum = sum + number
  68. Next i
  69.  
  70. Debug.Print sum
  71.  
  72. End Sub
  73.  
  74. 5*****************************************************
  75. Sub Example()
  76. For i = 1 To 1000
  77. If i Mod 10 = 7 Then
  78. Debug.Print i
  79. End If
  80. Next i
  81.  
  82. End Sub
  83.  
  84. 6*****************************************************
  85. Sub Example()
  86. Dim n As Integer
  87. n = Range("A1")
  88.  
  89. Dim oddSum, evenSum As Double
  90.  
  91. For i = 1 To n
  92. Dim number As Double
  93. number = Range("B" & i)
  94.  
  95. If i Mod 2 = 0 Then
  96. evenSum = evenSum + number
  97. Else
  98. oddSum = oddSum + number
  99. End If
  100. Next i
  101.  
  102. Debug.Print "Odd Sum = " & oddSum
  103. Debug.Print "Even Sum = " & evenSum
  104.  
  105. End Sub
  106.  
  107. 7*****************************************************
  108.  
  109. Sub Example()
  110. Dim n As Integer
  111. n = Range("A1")
  112.  
  113. For i = n To 1 Step -1
  114. Debug.Print i
  115. Next i
  116.  
  117. End Sub
  118.  
  119. 8*****************************************************
  120. Sub Example()
  121. Dim n As Integer
  122. n = Range("A1")
  123.  
  124. Dim p1, p2, p3 As Double
  125.  
  126. For i = 1 To n
  127. Dim number As Double
  128. number = Range("B" & i)
  129.  
  130. If number Mod 2 = 0 Then
  131. p1 = p1 + 1
  132. End If
  133. If number Mod 3 = 0 Then
  134. p2 = p2 + 1
  135. End If
  136. If number Mod 4 = 0 Then
  137. p3 = p3 + 1
  138. End If
  139. Next i
  140.  
  141. Debug.Print p1 / n * 100 & "%"
  142. Debug.Print p2 / n * 100 & "%"
  143. Debug.Print p3 / n * 100 & "%"
  144.  
  145. End Sub
  146.  
  147. 9*****************************************************
  148. Sub Example()
  149. Dim n, salary As Integer
  150. n = Range("A1")
  151. salary = Range("A2")
  152.  
  153. For i = 1 To n
  154. Dim inputLine As String
  155. inputLine = Range("B" & i)
  156.  
  157. Select Case inputLine
  158. Case "Facebook"
  159. salary = salary - 150
  160. Case "Instagram"
  161. salary = salary - 100
  162. Case "Reddit"
  163. salary = salary - 50
  164.  
  165. End Select
  166.  
  167. If salary <= 0 Then
  168. Exit For
  169. End If
  170.  
  171. Next i
  172.  
  173. Debug.Print salary
  174.  
  175. End Sub
  176.  
Advertisement
Add Comment
Please, Sign In to add comment