Arrays have me confused

P

Preschool Mike

I'm trying to use a userform to collect a list of words (i.e., 20) to later
diplay in a shape or textbox (as flash cards) in my presentation.. What's
the best way to collect and store my list? I know I could do something like
word1 = userform1.textbox1.text, word2 = userform1.textbox2.text and so on
but I thought an array would simplifiy things. The only problem is I'm
completely confused when it comes to using arrays. Also, I guess since I'm
here, how would I later retrieve my list for display in one textbox, one word
at a time (e.g., click my word button and the first word appears and click my
word button again and the second word appears).

Thanks so much!
 
T

Tamutka

' AirCode
Dim arayWords(1 to 20) as String

arayWords(1)= userform1.textbox1.text
arayWords(2)= userform1.textbox2.text
' and so on

' Or you could give each of the text boxes a
' predictable name, say MyText1, MyText2 and so on,
' then

Dim oCtl as Control
Dim x as long
x = 1
For each oCtl in Userform1.Controls
    If Instr(oCtl.Name,"MyText") > 0 Then
        arayWords(x) = .Item(x).text
        x = x+1
    End If
Next

To display them, something like this:

Sub MyWord()
' would need to have arayWords declared as public
' at top of module
  Static WordIndex as Long
  ' reset the counter if we've used all the words
  If WordIndex > 20 then WordIndex = 1
  MsgBox arayWords(WordIndex)
  WordIndex = WordIndex + 1
End Sub

==============================
PPT Frequently Asked Questionshttp://www.pptfaq.com/

PPTools add-ins for PowerPointhttp://www.pptools.com/

Hi,
I am confused and curious: this seems very interresteing, but I do not
know how and where to use. Can anyone explain me more deeply, please?
Or show me an example?
Thank you!
Natasa
 
D

David Marcovitz

Mike,

I'm guessing you read the section in my book about arrays and are still
confused so I'm not sure I can explain it any better, but I'll try.

You know what a variable is, a place to store one piece of information.
An array is simply a bunch of numbered boxes to store multiple pieces of
the same kind of information. For example if you want a bunch of words,
you could use:

Dim myword1 As String
Dim myword2 As String
Dim myword3 As String
....

I do this a lot because it is easy to understand. However, you might
want to simplify this by using an array. If you want an array with 20
items, you would do

Dim myword(20) As String

The tricky part about this is that it gives you 20 items, numbered 0
through 19:

myword(0), myword(1), myword(2), ..., myword(19)

Only a computer geek starts counting at 0 so this is confusing. Steve,
in his example, uses the nice trick to tell the computer that he wants
to start counting at 1:

Dim myword(1 To 20) As String

Now, you have:

myword(1), myword(2), myword(3), ..., myword(20)

This might confuse a computer geek, but it is much easier for the rest
of us.

Now, once you have an array, you can pretend that you don't have an
array and just access the elements of the array like you would a bunch
of individual variables:

myword(1) = "mom"
myword(2) = "dad"
myword(3) = "cat"
....
myword(20) = "noodle"

That will work fine, but it defeats the purpose of having an array
(except that you have only one simple Dim statement). The power of the
array is that you can access different elements by number. This is great
if you want to access one randomly (e.g., generate a random number
between 1 and 20, put it in the variable myRandNum and then access
myWord(myRandNum) ), or if you want to use a variable in any other way
to decide which array element to access (perhaps, based on which button
is pressed or which slide you are on). It is also great if you want to
cycle through the elements of the array:

For i = 1 To 20
MsgBox myWord(i)
Next i

This just displays each element of the array in succeeding message boxes.

Two other quick notes about arrays. Arrays can be multidimensional so
you could, for example, use an array to represent a checkerboard,
referring to rows and columns. But that gets a bit more complicated and
is beyond what you want to know now. You also don't need to know in
advance how many elements the array is going to have (possibly you want
to define it based on user input). For that you can read the section of
my book about ReDim.

I hope this makes it a little clearer, and I hope Steve's code does what
you want.

--David

I'm trying to use a userform to collect a list of words (i.e., 20) to later
diplay in a shape or textbox (as flash cards) in my presentation.. What's
the best way to collect and store my list? I know I could do something like
word1 = userform1.textbox1.text, word2 = userform1.textbox2.text and so on
but I thought an array would simplifiy things. The only problem is I'm
completely confused when it comes to using arrays. Also, I guess since I'm
here, how would I later retrieve my list for display in one textbox, one word
at a time (e.g., click my word button and the first word appears and click my
word button again and the second word appears).

Thanks so much!


--
David M. Marcovitz
Author of _Powerful PowerPoint for Educators_
http://www.PowerfulPowerPoint.com/
Microsoft PowerPoint MVP
Associate Professor, Loyola University Maryland
 
P

Preschool Mike

Thanks David for your patience and spelling that out for me. I guess my
confusion started with how do I get 20 text boxes from my userform to go into
one array. I get it if I would use something like an InputBox with a For
Loop: (e.g., it keeps asking until all 20 have been entered). I've read your
book over and over and have started reading Microsoft Visual Basic 2005 as
well as Joshn Walkenbach's, Excel 2007 Power Programming with VBA. Actually
I read Microsoft Visual Basic 2005 before I read yours. I've found that
after reading yours it has helped with understanding the others. Yours is by
far is an excellent place to start for anyone wanting to learn VB.
 
P

Preschool Mike

Steve I'm having a problem with part of your code. I get a message "Invalid
or uqualified reference" to the .Item(x).Text part of the code. Can you
help me fix this?

Thanks
--
Mike Mast
Special Education Preschool Teacher


 
P

Preschool Mike

That did the trick. I like how you coded it to start over when it gets to
the last word. Thanks for your help!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top