Page 1 of 2 12 LastLast
Results 1 to 10 of 15

Thread: For loop / Array Question
Thanks Thanks: 0 Likes Likes: 0

  1. #1
    New Member
    Join Date
    Oct 2011
    Posts
    19
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default For loop / Array Question

    So i'm supposed to generate a 5 digit number which i will compare to other numbers. I should then store it into a string variable:

    i = Rnd() * (99999 - 9999) + 9999
    number = LTrim(str(i))

    Then im supposed to ask how many numbers a person would like to generate to try to match to this number I already created. I am to use a for loop and store these numbers into a string array (Which im not sure how to do). Ill finish it up with a do loop that trys to match these numbers with the winning number up top. My problem is, im not sure how to generate the amount the user wants.
    I.E. how do I structure a For loop to generate the amount of numbers a person wants, and then store it into a string array. Thanks for reading.

  2. #2
    Board Regular Worf's Avatar
    Join Date
    Oct 2011
    Location
    Rio, Brazil
    Posts
    3,726
    Post Thanks / Like
    Mentioned
    6 Post(s)
    Tagged
    2 Thread(s)

    Question Re: For loop / Array Question

    Hi
    Like Jack the Ripper, let's do it part-wise...

    Is this the kind of string array you are after?
    Code:
    Sub EArray()
    Dim strArr$(5), msgvar$, i%, num#
    msgvar = " "
    For i = 1 To 5
        num = Rnd()
        strArr(i) = CStr(num)
        msgvar = msgvar & strArr(i) & "  "
    Next
    MsgBox msgvar
    End Sub

  3. #3
    Legend NateO's Avatar
    Join Date
    Feb 2002
    Location
    Minneapolis, Mn, USA
    Posts
    9,700
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: For loop / Array Question

    A small comment, your Array actually has six elements, so it might make sense to declare it as 1-based, e.g.,

    Code:
    Sub EArray()
    Dim strArr(1 To 5) As String, msgVar As String
    Dim i As Long, num As Double
    MsgBox LBound(strArr)
    For i = 1 To 5
        Let num = Rnd()
        Let strArr(i) = num
        Let msgVar = Join$(strArr, " ")
    Next
    MsgBox msgVar
    End Sub

  4. #4
    New Member
    Join Date
    Oct 2011
    Posts
    19
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: For loop / Array Question

    Thanks for the reply guys! ive run what youve given me and think I may have confused you as to what im looking for, Im a little slow with this stuff so bear with me.

    Im asking for user input, for example, how many cards they would like to buy.
    For each card the user wants to buy, i need to generate a 5 digit number. Then, with all these numbers stored in an array, I will see if they match the first winning 5 digit number ive already declared in the code.

    I know this will require a for loop I just dont know how to structure it. Thanks.

  5. #5
    Board Regular Worf's Avatar
    Join Date
    Oct 2011
    Location
    Rio, Brazil
    Posts
    3,726
    Post Thanks / Like
    Mentioned
    6 Post(s)
    Tagged
    2 Thread(s)

    Question Re: For loop / Array Question

    Hello Mr. Nate
    I always use Option Base 1, which did not appear on my post. Anyway, your piece of code is better.
    Talking about code, do you want to write it for Madhadda, or leave it to me?

  6. #6
    New Member
    Join Date
    Oct 2011
    Posts
    19
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: For loop / Array Question

    Hopefully its not too time consuming, Its not too tough im just not sure how to set it up, any advice is appreciated, thanks for the help so far guys.

  7. #7
    Legend NateO's Avatar
    Join Date
    Feb 2002
    Location
    Minneapolis, Mn, USA
    Posts
    9,700
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: For loop / Array Question

    Please don't call me Mr., Nate is all good, I didn't mean to be rude. Go ahead and write it for him - although I'm not sure why, it's pretty easy.

    Quote Originally Posted by Madhadda View Post
    Its not too tough im just not sure...

  8. #8
    Board Regular Worf's Avatar
    Join Date
    Oct 2011
    Location
    Rio, Brazil
    Posts
    3,726
    Post Thanks / Like
    Mentioned
    6 Post(s)
    Tagged
    2 Thread(s)

    Arrow Re: For loop / Array Question

    Well, at the Microsoft MVP page you are Mr. Nathan...

    Madhadda:

    I'll write it for you tomorrow, because it's 1:15 AM , bed time...

  9. #9
    MrExcel MVP Rick Rothstein's Avatar
    Join Date
    Apr 2011
    Location
    New Jersey, USA
    Posts
    35,239
    Post Thanks / Like
    Mentioned
    92 Post(s)
    Tagged
    33 Thread(s)

    Default Re: For loop / Array Question

    Quote Originally Posted by Worf View Post
    I always use Option Base 1
    Just a word of caution when/if you use the Split function... it always returns a zero-based array no matter what your Option Base setting is.

  10. #10
    Legend NateO's Avatar
    Join Date
    Feb 2002
    Location
    Minneapolis, Mn, USA
    Posts
    9,700
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: For loop / Array Question

    Coerced Byte Arrays will always give you a 0-based Array, too. E.g.,

    Code:
    Option Explicit
    Option Base 1
     
    Sub foo()
    Dim b() As Byte
    Let b = "Hello World"
    MsgBox LBound(b)
    End Sub
    I don't trust the Option Base setting, personally - I stick with defining/testing my boundaries.

    In terms of my profile... That wasn't up to me. Let's just stick with Nate.
    Last edited by NateO; Nov 6th, 2011 at 10:43 PM.

Some videos you may like

User Tag List

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •