Home » excel » vba – How to use a counter as an argument in functions?

vba – How to use a counter as an argument in functions?

Posted by: admin May 14, 2020 Leave a comment

Questions:

a standard loop in Excel VBA goes like this:

dim i as integer
for i = 1 to 100 
<do program>
next
end sub

Now, my question is: how can I use the counter, i, as an argument in the program?

example:

dim i as integer
for i = 1 to 100 

If Range("Ci") = 0 Then
Rows("i:i").Select
Rows.Delete

next
end sub
How to&Answers:

You should be able to do something like this:

 dim i as integer
 for i = 1 to 100 

      If Range("C" & i) = 0 Then
           Rows(i & ":" & i).Select
           Rows.Delete
      end if
 next
 end sub

The & is the concatenation operator which is used to add a string onto another string.

Now i is obviously a number in the above example, but VBScript is smart enough to know what you’re doing and, for example, just stick "C" and the current value of i together.