Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 100
Default non-linear step count loop syntax?

I have an existing workbook that requires modification. The workbook has
arrays that were built for earlier needs, and all of that data needs to be
retained (e.g. I can't change the arrays or their contents). However, I have
some code that used to loop through all elements of some arrays, and now I
need to be more selective.

Simplified example- my old code was

For x=3 to 10
'do stuff
Next

and now I need it to be
For X=3,5,7-10
'do stuff
Next

I've worked with linear loops and 1 step values, but I'm not sure how to
approach this type of non-linear loop- and I can't recall seeing any
examples anywhere that deal with this type of situation. What is the proper
syntax to use for the non-linear loop count? Or do I need to build a
separate array of my values (3,5,7,8,9,10 in this example) and reference it
as

MyArray= Array(3,5,7,8,9,10)
For Y = 1 to 6
X=MyArray(Y)
'do stuff
Next

Thanks,
Keith


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 119
Default non-linear step count loop syntax?

On Apr 8, 10:22*am, "Ker_01" wrote:
and now I need it to be
For X=3,5,7-10
*'do stuff
Next


Welcome to IF/GOTO hell. VB's control structures are not exactly it's
strong suit.

Maury
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default non-linear step count loop syntax?

You could do it this way...

Dim X As Variant
For Each X In Array(3, 5, 7, 8, 9, 10)
' do stuff
Next

The value of X at each loop will be 3, 5, 7, 8, 9, 10 in order.

Rick


"Ker_01" wrote in message
...
I have an existing workbook that requires modification. The workbook has
arrays that were built for earlier needs, and all of that data needs to be
retained (e.g. I can't change the arrays or their contents). However, I
have some code that used to loop through all elements of some arrays, and
now I need to be more selective.

Simplified example- my old code was

For x=3 to 10
'do stuff
Next

and now I need it to be
For X=3,5,7-10
'do stuff
Next

I've worked with linear loops and 1 step values, but I'm not sure how to
approach this type of non-linear loop- and I can't recall seeing any
examples anywhere that deal with this type of situation. What is the
proper syntax to use for the non-linear loop count? Or do I need to build
a separate array of my values (3,5,7,8,9,10 in this example) and reference
it as

MyArray= Array(3,5,7,8,9,10)
For Y = 1 to 6
X=MyArray(Y)
'do stuff
Next

Thanks,
Keith


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 100
Default non-linear step count loop syntax?

Thanks Rick!
That is one step better than my separate array of values, and more eloquent.
Best regards,
Keith

"Rick Rothstein (MVP - VB)" wrote in
message ...
You could do it this way...

Dim X As Variant
For Each X In Array(3, 5, 7, 8, 9, 10)
' do stuff
Next

The value of X at each loop will be 3, 5, 7, 8, 9, 10 in order.

Rick


"Ker_01" wrote in message
...
I have an existing workbook that requires modification. The workbook has
arrays that were built for earlier needs, and all of that data needs to be
retained (e.g. I can't change the arrays or their contents). However, I
have some code that used to loop through all elements of some arrays, and
now I need to be more selective.

Simplified example- my old code was

For x=3 to 10
'do stuff
Next

and now I need it to be
For X=3,5,7-10
'do stuff
Next

I've worked with linear loops and 1 step values, but I'm not sure how to
approach this type of non-linear loop- and I can't recall seeing any
examples anywhere that deal with this type of situation. What is the
proper syntax to use for the non-linear loop count? Or do I need to
build a separate array of my values (3,5,7,8,9,10 in this example) and
reference it as

MyArray= Array(3,5,7,8,9,10)
For Y = 1 to 6
X=MyArray(Y)
'do stuff
Next

Thanks,
Keith




Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Syntax for stopping a Do Loop? bondjel Excel Discussion (Misc queries) 5 March 22nd 10 01:35 PM
Do...Loop syntax help? DB74 Excel Programming 5 September 4th 07 07:04 PM
Do loop until, step down row - copy paste - not what it seems. justagrunt Excel Programming 0 December 8th 06 01:21 AM
loop syntax Knox Excel Programming 2 May 17th 06 06:57 PM


All times are GMT +1. The time now is 11:32 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"