Step -1 method

  • Thread starter Thread starter K
  • Start date Start date
K

K

Hi all, Just for knowledge that what kind of work "Step - 1" method
do and when it is used. Pleae can any friend can explain briefly as i
wasn't able to find much information about this method.
 
Step is optional numeric expression which denotes the amount by which counter
is incremented each time through the loop.

For intTemp = 10 to 1 Step-1
Next

will have intTemp values in the order 10,9,8,7,6,...

and Step -2 will have 10,8,6,4,2



If this post helps click Yes
 
It's very useful when you want to process a range of rows from the bottom to the
top or a range of columns from the right to the left.

Dim iRow as long
with activesheet
for irow = 20 to 10 step -1
if .cells(irow,"A").value = "" then
.rows(irow).delete
end if
next irow
end with

or

Dim iCol as long
dim FirstCol as long
Dim LastCol as long
with activesheet
firstcol = 1
lastcol = .cells(1,.columns.count).end(xltoleft).column

for icol = lastcol to firstcol step -1
if .cells(1,icol).value = "" then
.columns(icol).delete
end if
next icol
end with

Or just when you want to loop through any old array in reverse order.

dim ictr as long
dim myArr as variant
myarr = array("something", "else","would", "go here")
for ictr = ubound(myArr) to lbound(myarr) step -1
msgbox myarr(ictr)
next ictr

Or loop through a string
dim iCtr as long
dim myStr as string

mystr = "Just for knowledge"
for ictr = len(mystr) to 1 step -1
msgbox mid(mystr, ictr, 1)
next ictr
 
Back
Top