Consulting

Results 1 to 12 of 12

Thread: How to sort numbers in asceding order?

  1. #1

    How to sort numbers in asceding order?

    This is Lottery program which generate random numbers, but output is not sorted. How to sort numbers in asceding order?
    This is VBA Express.

    Public
    Class ltaForm

    PrivateSub GetNumbersToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GetNumbersToolStripMenuItem.Click
    Dim intForLoopCtr AsInteger = 0
    Dim blnFullSetComplete AsBoolean = False
    Dim intRndNo AsInteger = 0
    Dim strDisplayString AsString = ""
    Dim intNoOfValidPics AsInteger = 0
    Dim aintLotteryArray(10) As Array
    Dim intNumberCount AsInteger = 0
    Dim strTestString AsString = "_"
    If txtFullSet.Text = ""Then
    MessageBox.Show("You must specify how many numbers " & _
    "make up a full set.")
    Return
    EndIf
    If IsNumeric(txtFullSet.Text) = FalseThen
    MessageBox.Show("You must specify numeric input when " & _
    "specifying how many numbers make up a full set.")
    Return
    EndIf
    For intForLoopCtr = 1 ToCInt(txtNoPics.Text)
    DoUntil blnFullSetComplete = True
    Randomize()
    intRndNo = _
    FormatNumber(Int((txtNoRange.Text * Rnd()) + 1))
    If InStr(strTestString, _
    Convert.ToString(
    "_" & intRndNo & "_")) = 0 Then
    strDisplayString = strDisplayString & " " & _
    intRndNo & ControlChars.Tab
    intNoOfValidPics = intNoOfValidPics + 1
    strTestString = strTestString & intRndNo &
    "_"
    EndIf
    If intNoOfValidPics = Int32.Parse(txtFullSet.Text) Then
    blnFullSetComplete = True
    strDisplayString = strDisplayString & _
    ControlChars.NewLine & ControlChars.NewLine
    strTestString =
    "_"


    EndIf

    Loop
    blnFullSetComplete = False
    intNoOfValidPics = 0
    Next
    txtOutput.Text = strDisplayString
    GetNumbersToolStripMenuItem.Enabled =
    False
    ClearToolStripMenuItem.Enabled = True






    EndSub

  2. #2
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,059
    Location
    What does the macro recorder suggest?
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  3. #3
    This is vbaexpress not excel.

  4. #4
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,059
    Location
    You are absolutely correct..... so i'll ask you again. What did the macro recorder suggest? If you use the recorder then look at the code it may well give you a hint as to how you might proceed with the code.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  5. #5
    No I don't use macro recorder. This code is from book "Microsoft Visual Basic 2005 Express Edition Programming for the Absolute Beginner".

    Code work fine and generate random numbers, but numbres are not sorted.

  6. #6
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    It's not clear to me what the output of your routine is. Is your routine returning an array or a delimited string?

    This is a bubble sort routine for sorting an array.

    For i = 1 to UBound(myArray) - 1
        For j = i+1 to UBound(myArray)
            If myArray(j) < myArray(i) Then
               tempVal = myArray(i)
               myArray(i) = myArray(j)
               myArray(j) = tempVal
            End If
         Next j
    Next i

  7. #7
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    Your code is Visual Basic, not Visual Basic for Applications. VB is an application that uses VBA; Excel is a different application that uses VBA. Which do you want to use?
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  8. #8
    Maybe this is wrong forum. I have Microsoft Visual Basic 2005 express edition, which can run this program.
    If this is wrong forum please just delete thread!

  9. #9
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    As your question has nothing to do with Excel it is really in the wrong place but I will try to help.

    Your code generates several (txtNoPics.Text) sequences of (random) numbers. You build up a string (strDisplayString) of the sequences, delimited by newline characters, as you go along.

    Each individual set of numbers is also built into a second string (strTestString) which is only used for deduplication and is discarded after each sequence is finished.

    You need to move the building of the Display String outside the inner loop so that you can sort each generated sequence before you add it to the display string, something along these lines ..

    [vba]
    For intForLoopCtr = 1 To CInt(txtNoPics.Text)
    Do Until blnFullSetComplete = True
    Randomize()
    intRndNo = _
    FormatNumber(Int((txtNoRange.Text * Rnd()) + 1))
    If InStr(strTestString, _
    Convert.ToString("_" & intRndNo & "_")) = 0 Then
    ' Next line removed from this loop
    ' strDisplayString = strDisplayString & " " & _
    ' intRndNo & ControlChars.Tab
    intNoOfValidPics = intNoOfValidPics + 1
    strTestString = strTestString & intRndNo & "_"
    End If
    If intNoOfValidPics = Int32.Parse(txtFullSet.Text) Then
    blnFullSetComplete = True
    ' Now to sort the generated sequence
    ' First put in an array
    myArray = Split(Trim(Replace(strTestString, "_", " ")))
    ' then use mikerickson's bubble sort (adjusted to start at zero)
    For i = LBound(myArray) To UBound(myArray) - 1
    For j = i + 1 To UBound(myArray)
    If myArray(j) < myArray(i) Then
    tempVal = myArray(i)
    myArray(i) = myArray(j)
    myArray(j) = tempVal
    End If
    Next j
    Next i
    ' Now add the sorted list to the display string
    strDisplayString = strDisplayString & " " & _
    Join(myArray, ControlChars.Tab) & ControlChars.NewLine
    ' all replacing what you had before
    'strDisplayString = strDisplayString & _
    'ControlChars.NewLine & ControlChars.NewLine
    strTestString = "_"


    End If

    Loop

    blnFullSetComplete = False
    intNoOfValidPics = 0
    Next



    [/vba]

    My apologies if I've got some of the VB (as opposed to VBA) bits wrong
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  10. #10
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    A slight correction ..

    The results of Split are string values so you should change ..
    [VBA]If myArray(j) < myArray(i) Then[/VBA]
    to ..
    [VBA]If CInt(myArray(j)) < cint(myArray(i)) Then [/VBA]
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  11. #11

    Solved!

    It works!
    Thanks Tony Jollans for help and explanation!
    So this problem is solved!

  12. #12
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    My pleasure
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

Posting Permissions

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