Results 1 to 2 of 2

Choose 4 Random Numbers if they are not textbox1 or textbox2

Threaded View

blitz74 Choose 4 Random Numbers if... 10-03-2012, 02:15 AM
blitz74 Re: Choose 4 Random Numbers... 10-03-2012, 02:41 AM
  1. #1
    Registered User
    Join Date
    09-21-2012
    Location
    antartica
    MS-Off Ver
    Excel 2010
    Posts
    2

    Choose 4 Random Numbers if they are not textbox1 or textbox2

    Hi there people

    I have 2 textboxes (FavNum1) and (FavNum2) on a userform that you can enter your 2 favorite numbers (1 to 45)..

    I can successfully generate 4 random numbers (without repeats).. but where I'm having the problem is the randomonly
    generated numbers from the array CAN NOT match textboxes (FavNum1) and (FavNum2)

    I think I have to put a loop or 'do while' just before the line below, but it's not working..

    If arrRandom(I) = FavNum1 Or FavNum2 Then
        arrRandom(I) = Int((intHighest + 1 - intLowest) * Rnd) + intLowest
    End If
    I've been trying to nut this out for the past 4 hours and it's driving me bananas. Any help or guidance
    would be really grateful.

    Kind regards

    Steve


    '----------------------------------------
    ' START OF THE CODE
    '----------------------------------------
    
    Const intLowest As Integer = 1 'Lowest number required
    Const intHighest As Integer = 45 'Highest number required
    Const intHowMany As Integer = 4 'Number of different values to return
    
    Dim booFlag As Boolean
    Dim NumOfTicketsInput As Integer ' Number of games / line to produce
    Dim FavNum1 As Integer
    Dim FavNum2 As Integer
    
    NumOfTicketsInput = TextBox1.Value
    FavNum1 = TextBox2.Value
    FavNum2 = TextBox3.Value
    
    ' -----------------------------------------------------------------------------------
    
    For NumOfTickets = 1 To NumOfTicketsInput
    
    'Check to prevent infinite loop
    
    If intHowMany > (intHighest - intLowest + 1) Then
    MsgBox "Too many numbers or too small a range."
    Exit Sub
    End If
    
    'Set up array to hold the random value
    
    Dim arrRandom(1 To intHowMany) As Integer
    
    Randomize 'Resets random seed
    
    'Generate the first random value
    
    arrRandom(1) = Int((intHighest + 1 - intLowest) * Rnd) + intLowest
    
    
    If arrRandom(1) = FavNum1 Then ' choose another random number if it is the same as FavNum1
    
        arrRandom(1) = Int((intHighest + 1 - intLowest) * Rnd) + intLowest
    End If
    
    
    'Generate subsequent random values and ensure no dupes
    
    For I = 2 To intHowMany
    
    Do
    
    booFlag = False
    
    arrRandom(I) = Int((intHighest + 1 - intLowest) * Rnd) + intLowest
    
    ' ====================================================================
    ' THIS IS WHERE THE PROBLEM IS -- DO I NEED TO PUT A LOOP / DO WHILE BELOW ??
    
    If arrRandom(I) = FavNum1 Or FavNum2 Then
        arrRandom(I) = Int((intHighest + 1 - intLowest) * Rnd) + intLowest
    End If
    
    ' ====================================================================
    
    For j = 1 To I - 1
    
    If arrRandom(j) = arrRandom(I) Then booFlag = True
    
    Next j
    
    Loop Until booFlag = False 'Ensures value not kept if it is a dupe
    
    Next I
    
    'Write the values back to the worksheet - change "B1" reference as required
    
    For I = 1 To intHowMany
    
    ActiveWorkbook.Worksheets("Sheet1").Range("A" & (NumOfTickets)) = FavNum1
    ActiveWorkbook.Worksheets("Sheet1").Range("B" & (NumOfTickets)) = FavNum2
    ActiveWorkbook.Worksheets("Sheet1").Range("C" & (NumOfTickets)).Offset(0, I).Value = arrRandom(I)
    
    Next I
       
    Randomize
    
    Next NumOfTickets
    
    ' ---------------------------------------------------------------------------------------
    
    ' sort all 6 numbers in row from left to right in worksheet
    
    Dim iRow As Long
    Dim Firstrow As Long
    Dim lastrow As Long
    
    With ActiveSheet
      Firstrow = 1 'change to 2 if there are headings
      lastrow = .Cells(.Rows.Count, "B").End(xlUp).Row
    
      For iRow = Firstrow To lastrow
        With .Cells(iRow, "A").Resize(1, 7)
          .Sort Key1:=.Columns(1), _
          Order1:=xlAscending, _
          Header:=xlNo, _
          OrderCustom:=1, _
          MatchCase:=False, _
          Orientation:=xlLeftToRight
        End With
      Next iRow
    End With
    Last edited by vlady; 10-03-2012 at 02:43 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1