Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 172
Default use different string variables in FOR loop

I would like to create a FOR loop that uses many different string
values as the loop variable, maybe 20-30 different values.

What kind of structure is required to loop through a list of string
varibles like this:

red
green
blue
orange
and so on....

Thanks


John Keith

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10,124
Default use different string variables in FOR loop


As always, post your efforts for comments

--
Don Guillett
Microsoft MVP Excel
SalesAid Software

"John Keith" wrote in message
...
I would like to create a FOR loop that uses many different string
values as the loop variable, maybe 20-30 different values.

What kind of structure is required to loop through a list of string
varibles like this:

red
green
blue
orange
and so on....

Thanks


John Keith


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 172
Default use different string variables in FOR loop

On Sat, 20 Sep 2008 11:21:10 -0500, "Don Guillett"
wrote:


As always, post your efforts for comments


Sure, for you and Rick I'll provide a little more detail for the
discussion although the solution posted by
is just what I was looking for.

I had a lot of inline repetitive code that contained the following:

Sheets.Add Type:=xlWorksheet
ActiveSheet.Name = "Red"

This code and many other steps were identical and could clearly be
condensed inside a loop, I just didn't know how to structure the loop
with string names that changed.

Thanks for the help!




John Keith

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,934
Default use different string variables in FOR loop

More details about what you plan to do with the items as you loop through
them would be helpful. If, for example, you are just trying to see if one
the loop-items is equal to some other variable's content, you may not need
to use a loop at all. As with all programming, the code approach you would
use to solve the problem depends on what the problem you are trying to solve
is... if you don't tells us what that is, we could guess forever and never
get it right.

--
Rick (MVP - Excel)


"John Keith" wrote in message
...
I would like to create a FOR loop that uses many different string
values as the loop variable, maybe 20-30 different values.

What kind of structure is required to loop through a list of string
varibles like this:

red
green
blue
orange
and so on....

Thanks


John Keith


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default use different string variables in FOR loop

A For loop can only use a number for the count. However you can set up
an array with the strings in it, then use the loop variable to count
through the elements of the array.

<Code

Dim words(4) As String
words(0) = "red"
words(1) = "green"
words(2) = "blue"
words(3) = "orange"

' This will print each of those words in order
For i = 0 To UBound(words) - 1
Debug.Print words(i)
Next

</Code

As long as you can get the words you want to cycle through into an
array, you can count through them in a loop this way.

Does that help?



On Sep 20, 5:14*pm, John Keith wrote:
I would like to create a FOR loop that uses many different string
values as the loop variable, maybe 20-30 different values.

What kind of structure is required to loop through a list of string
varibles like this:

red
green
blue
orange
and so on....

Thanks

John Keith




  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,934
Default use different string variables in FOR loop

A For loop can only use a number for the count.

That is not entirely true... you can use a For Each loop to iterate a
collection or an array without referring to any numerical values. For
example, here is a rewrite of your code to demonstrate this...

<Code
Dim Word As Variant
For Each Word In Array("Red", "Green", "Blue", "Orange")
Debug.Print Word
Next
</Code

--
Rick (MVP - Excel)


wrote in message
...
A For loop can only use a number for the count. However you can set up
an array with the strings in it, then use the loop variable to count
through the elements of the array.

<Code

Dim words(4) As String
words(0) = "red"
words(1) = "green"
words(2) = "blue"
words(3) = "orange"

' This will print each of those words in order
For i = 0 To UBound(words) - 1
Debug.Print words(i)
Next

</Code

As long as you can get the words you want to cycle through into an
array, you can count through them in a loop this way.

Does that help?



On Sep 20, 5:14 pm, John Keith wrote:
I would like to create a FOR loop that uses many different string
values as the loop variable, maybe 20-30 different values.

What kind of structure is required to loop through a list of string
varibles like this:

red
green
blue
orange
and so on....

Thanks

John Keith


  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 172
Default use different string variables in FOR loop

On Sat, 20 Sep 2008 17:43:44 -0400, "Rick Rothstein"
wrote:

A For loop can only use a number for the count.


That is not entirely true... you can use a For Each loop to iterate a
collection or an array without referring to any numerical values. For
example, here is a rewrite of your code to demonstrate this...

<Code
Dim Word As Variant
For Each Word In Array("Red", "Green", "Blue", "Orange")
Debug.Print Word
Next
</Code


This solution is interesting to me as well and may get used in the
future, but the other solution posted gives me a better opportunity to
put a comment after each string value so I can better document what is
being done.

Thanks for the idea.



John Keith

  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 172
Default use different string variables in FOR loop

On Sat, 20 Sep 2008 12:34:33 -0700 (PDT),
wrote:

Does that help?


That is exactly what I was looking for!

Thank you!


John 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
Loop thru variables Madiya Excel Programming 8 October 17th 07 12:57 PM
evaluate and variables and loop... OKROB Excel Programming 1 January 10th 07 09:36 PM
Non Static Variables in a For...Next Loop Jess[_2_] Excel Programming 16 December 29th 06 10:00 PM
Is it possible to create variables in a loop.. shishi Excel Programming 2 February 3rd 06 07:21 PM
Write variables from a loop, once only Novaloc Excel Programming 1 October 11th 05 04:10 AM


All times are GMT +1. The time now is 05:25 PM.

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

About Us

"It's about Microsoft Excel"