Displaying random unique possibilities from rows from a table until it meets conditions

beginnervba

New Member
Joined
Jan 10, 2022
Messages
2
Office Version
  1. 2016
Platform
  1. Windows
I want to display random different possibilities from a specific data table. I am trying to grab random unique (cannot be used more than once) IDs from column "A" (about 100 IDs in this field), and making sure criteria is met from columns "B"
IDColourNumber
111Red1.5
112Blue2
113Green1
114Blue1.5
115Yellow2
116Blue1
and "C"(Usually only whole number or .5 decimal numbers).
Example list below, but very shortened format. I want to have total number (column "C") equal 19.5 or 20, and have:
  1. Red from column "B" to be > 5 in column "C".
  2. Green from column "B" have at least 2 in column "C"
  3. Yellow from column "C" cannot be more than 7 in column "C"
I want it so that every time I press a button, it will have another random possibility to meet a specific criteria.

I have been trying to freestyle with the little knowledge that I have and have the below so far. I think it is completely wrong, but I was wondering if this was in the right direction (What loop should I be doing etc. ).
VBA Code:
Sub RandomPossibilities()

Dim NoOfIDs As Long
Dim RandomNumber As Integer
Dim IDs(), Colour() As String 
Dim i As Byte
Dim CellsOut As Long 
Dim ArI As Byte 
Dim RedCount, BlueCount, YellowCount, GreenCount, TotalCount As Variant

Application.ScreenUpdating = False
RedCount = 0
BlueCount = 0
GreenCount = 0
YellowCount = 0
TotalCount = 0
CellsOut = 6
ReDim IDs(1 To 20) 
ReDim Colour(1 To 20)
NoOfIDs = Application.CountA(Range("A:A")) - 1 

i = 1
Do Until (TotalCount = 20 Or TotalCount = 19.5) And RedCount >= 5 And GreenCount >= 2 And YellowCount <= 7

RandomNo:
    RandomNumber = Application.RandBetween(2, NoOfIDs + 1)
    For ArI = LBound(IDs) To UBound(IDs)
        If IDs(ArI) = Cells(RandomNumber, 1).Value Then
            GoTo RandomNo
        End If
        If Cells(RandomNumber, 2).Value = "Not Available"
            GoTo RandomNo
        End If
     Next ArI
    
 IDs(i) = Cells(RandomNumber, 1).Value 
 Colour(i) = Cells(RandomNumber, 2).Value
    
 If Cells(RandomNumber, 2).Value = "Red" Then
     RedCount = RedCount + Cells(RandomNumber, 3).Value
 ElseIf Cells(RandomNumber, 2).Value = "Yellow" Then
     YellowCount = YellowCount + Cells(RandomNumber, 3).Value
 ElseIf Cells(RandomNumber, 2).Value = "Blue" Then
     BlueCount = BlueCount + Cells(RandomNumber, 3).Value
 ElseIf Cells(RandomNumber, 2).Value = "Green" Then
     GreenCount = GreenCount + Cells(RandomNumber, 3).Value
 End If
    

 If TotalCount = 20 Or TotalCount = 19.5 Then
    If RedCount >= 5 Then
       i = 0
       RedCount = 0
       BlueCount = 0
       GreenCount = 0
       YellowCount = 0
       TotalCount = 0
    End If
    If GreenCount >= 2 Then
       i = 0
       RedCount = 0
       BlueCount = 0
       GreenCount = 0
       YellowCount = 0
       TotalCount = 0
    End If
    If YellowCount <= 7 Then
       i = 0
       RedCount = 0
       BlueCount = 0
       GreenCount = 0
       YellowCount = 0
       TotalCount = 0
    End If
 End If
    
 If TotalCount >= 9 Then
    If RedCount >= 5 Then
       i = 0
       RedCount = 0
       BlueCount = 0
       GreenCount = 0
       YellowCount = 0
       TotalCount = 0
    End If
 If GreenCount >=2 Then
       i = 0
       RedCount = 0
       BlueCount = 0
       GreenCount = 0
       YellowCount = 0
       TotalCount = 0
 End If
 If YellowCount <=7 2 Then
       i = 0
       RedCount = 0
       BlueCount = 0
       GreenCount = 0
      YellowCount = 0
       TotalCount = 0
    End If
       i = 0
       RedCount = 0
       BlueCount = 0
       GreenCount = 0
       YellowCount = 0
    TotalCount = 0
 End If
    i = i + 1

TotalCount = GreenCount + BlueCount + YellowCount + RedCount
    
Loop


For ArI = LBound(IDs) To UBound(IDs)
    Cells(CellsOut + 2, 6) = IDs(ArI)
    Cells(CellsOut + 2, 7) = Colour(ArI)
    CellsOut = CellsOut + 1
Next ArI
Cells(8, 9) = YellowCount
Cells(9, 9) = BlueCount
Cells(10, 9) = GreenCount
Cells(11, 9) = RedCount
Cells(12, 9) = TotalCount
Application.ScreenUpdating = True
End Sub
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
VBA Code:
Option Compare Text

Sub RandomPos()
     Dim Output(), bRed, iRed, bBlue, IBlue, bYellow, iYellow, iGreen, bGreen
     With Range("A1").CurrentRegion
          arr = .Offset(1).Resize(.Rows.Count - 1, 3).Value     'read your data to an array, x rows * 3 columns width
     End With

     Do
          ptr = ptr + 1
          DoEvents

          ReDim Output(1 To 20, 1 To 5)
          rand = Application.WorksheetFunction.RandArray(UBound(arr) - 1)     ' array with x random numbers between 0 and 1
          For i = 1 To UBound(Output)                           'unique random select 20 numbers
               r = Application.Match(WorksheetFunction.Small(rand, i), rand, 0)     'position of the 1-smallest number in your list
               Output(i, 1) = arr(r, 1)                         'random name
               Select Case arr(r, 2)                            'select on color
                    Case "Red": Output(i, 2) = arr(r, 3)        '2nd column
                    Case "Blue": Output(i, 3) = arr(r, 3)       '3rd column
                    Case "Yellow": Output(i, 4) = arr(r, 3)     '4th column
                    Case "Green": Output(i, 5) = arr(r, 3)      '4th column
                    Case Else: MsgBox "wrong color " & arr(r, 2)
               End Select
          Next
          Range("E4").Resize(UBound(Output), UBound(Output, 2)).Value = Output

          iRed = Application.Sum(Application.Index(Output, 0, 2)): bRed = (iRed > 5)
          iGreen = Application.Sum(Application.Index(Output, 0, 5)): bGreen = (iGreen > 2)
          iYellow = Application.Sum(Application.Index(Output, 0, 4)): bYellow = (iYellow <= 7)

     Loop While (Not bRed Or Not bGreen Or Not bYellow) And ptr < 1000
     
     If bRed + bGreen + bYellow <> -3 Then MsgBox "no combination found after " & ptr & " loops" Else MsgBox "ready after " & ptr & " loops "

End Sub
Map1
ABCDEFGHIJ
1IDColourNumbercount31403
2111Red1,5sum62404
3112Blue2IDRedBlueYellowgreen
4113Green12091
5114Blue1,51432
6115Yellow21802
7116Blue11562
Blad2
Cell Formulas
RangeFormula
F1:I1F1=COUNT(F$4:F$23)
F2:I2F2=SUM(F$4:F$23)
 
Upvote 0
error : it's not -1, so delete that part
VBA Code:
rand = Application.WorksheetFunction.RandArray(UBound(arr) - 1)     ' array with x random numbers between 0 and 1
correct :
rand = Application.WorksheetFunction.RandArray(UBound(arr) )
 
Upvote 0
error : it's not -1, so delete that part
VBA Code:
rand = Application.WorksheetFunction.RandArray(UBound(arr) - 1)     ' array with x random numbers between 0 and 1
correct :
rand = Application.WorksheetFunction.RandArray(UBound(arr) )
Thanks for the quick reply!

I am getting error "Unable to get the RandArray property of the WorksheetFunction class" and the code above highlighted. I was wondering if i did something wrong?
 
Upvote 0
indeed, randarray is for newer excelversions, solution, make that array yourself with a small loop
VBA Code:
Option Compare Text

Sub RandomPos()

     Dim Output(), bRed, iRed, bBlue, IBlue, bYellow, iYellow, iGreen, bGreen, rand()
     
     With Range("A1").CurrentRegion
          arr = .Offset(1).Resize(.Rows.Count - 1, 3).Value     'read your data to an array, x rows * 3 columns width
     End With

     ReDim rand(1 To UBound(arr))                               'make array so big

     Do
          ptr = ptr + 1
          DoEvents

          ReDim Output(1 To 20, 1 To 5) 'clear preivous Output
          Randomize                                             'randomizer
          For i = 1 To UBound(rand): rand(i) = Rnd: Next        'fill yourself that array with x random numbers

          For i = 1 To UBound(Output)                           'unique random select 20 numbers
               r = Application.Match(WorksheetFunction.Small(rand, i), rand, 0)     'position of the 1-smallest number in your list
               Output(i, 1) = arr(r, 1)                         'random name
               Select Case arr(r, 2)                            'select on color
                    Case "Red": Output(i, 2) = arr(r, 3)        '2nd column
                    Case "Blue": Output(i, 3) = arr(r, 3)       '3rd column
                    Case "Yellow": Output(i, 4) = arr(r, 3)     '4th column
                    Case "Green": Output(i, 5) = arr(r, 3)      '4th column
                    Case Else: MsgBox "wrong color " & arr(r, 2)
               End Select
          Next
          Range("E4").Resize(UBound(Output), UBound(Output, 2)).Value = Output

          iRed = Application.Sum(Application.Index(Output, 0, 2)): bRed = (iRed > 5)
          iGreen = Application.Sum(Application.Index(Output, 0, 5)): bGreen = (iGreen > 2)
          iYellow = Application.Sum(Application.Index(Output, 0, 4)): bYellow = (iYellow <= 7)

     Loop While (Not bRed Or Not bGreen Or Not bYellow) And ptr < 1000

     If bRed + bGreen + bYellow <> -3 Then MsgBox "no combination found after " & ptr & " loops" Else MsgBox "ready after " & ptr & " loops "

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,385
Messages
6,119,209
Members
448,874
Latest member
b1step2far

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top