If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below. |
|
|
Thread Tools | Display Modes |
#1
|
|||
|
|||
Arrays have me confused
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! -- Mike Mast Special Education Preschool Teacher |
#2
|
|||
|
|||
Arrays have me confused
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 On 4/8/10 7:54 PM, Preschool Mike wrote: 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 |
#3
|
|||
|
|||
Arrays have me confused
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. -- Mike Mast Special Education Preschool Teacher "David Marcovitz" wrote: 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 On 4/8/10 7:54 PM, Preschool Mike wrote: 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 . |
Thread Tools | |
Display Modes | |
|
|