PGSystemTester

Excel VBA Loops (Simple)

Sep 25th, 2017
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'BASIC VBA Loops
  2. Dim C As Integer
  3.  
  4. 'Option 1: Do UNTIL condition is TRUE (i.e. checks at end).
  5. Do
  6. C = C + 1
  7. Loop Until C > 10
  8.  
  9. 'Option 2: Do WHILE condition is TRUE (i.e. checks at beginning).
  10. Do While C < 11
  11. C = C + 1
  12. Loop
  13.  
  14. 'Option 3: Looping in a set of integers (example of 1 to 100).
  15. '"Step" defaults to +1, but can be any positive or negative integer
  16. For C = 1 To 100 Step 1
  17.     Range("A" &  C).VALUE = "BOOM" & C
  18. Next C
  19.  
  20. 'Option 4: Loop within a range.
  21. Dim rCell As Range
  22.  
  23. For Each rCell In Range("A1:Z100").Cells
  24.     rCell.clearcontents
  25.  
  26. Next rCell
Add Comment
Please, Sign In to add comment