Number Shuffle - Greater than last number, No repeats.

JBowman

New Member
Joined
Jan 21, 2021
Messages
6
Office Version
  1. 2019
Platform
  1. Windows
  2. Mobile
  3. Web
Hi All,
I am using Excel 2019 & have a requirement to shuffle a fixed set of numbers in column B so that they are greater than the numbers in column A.
The numbers in column B must only be all those numbers, (so a RAND function wont work) and must be all inclusive between 10 & 29 without duplication or a number missing.
Additionally the distance between the numbers in column A & B cant be greater than 20, i.e. the number in A1 is "1" and so the number in B1 cant be greater than "21", it must be between 2 & 20 etc..

I have played around with this without much success for a long time & now i need some help!!

Number Shuffle.xlsx
ABCDEFGHIJKLMN
1110
2211
3312
4413
5514
6615
7716
8817
9918
101019
111120
121221
131322
141423
151524
161625
171726
181827
191928
202029
21
22
23
24
25
26
27
Sheet1
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Would a VBA solution be acceptable?

I put the results in column C.
Columns D to W are just so I can easily check for duplicates and are not part of the solution.
Basically, you click on the (optional) button and you get a new set of numbers.

Capture.JPG
 

Attachments

  • Capture.JPG
    Capture.JPG
    157.4 KB · Views: 4
Upvote 0
Hi, this looks like it could work, how can i download your solution to try it out?
 
Upvote 0
Would a VBA solution be acceptable?

I put the results in column C.
Columns D to W are just so I can easily check for duplicates and are not part of the solution.
Basically, you click on the (optional) button and you get a new set of numbers.

View attachment 33053
Hi, this looks like it could work, how can i download your solution to try it out?
 
Upvote 0
Hi,

You will need to create a new Module and paste in the code below.
You may need to go into Tools->References to include mscorlib.dll but it worked for me without having to do that.
There is an issue in that that the numbers for rows 1 to 9 can be chosen without using the number 10 (or 1-10 without 11 etc) but 10 cannot be used elsewhere. If an invalid set of numbers is found the code loops until a valid set is discovered. It usually only takes one iteration but it has taken 8 on a couple of occasions.

The numbers are chosen for the top and bottom rows first and then it works towards the middle.



VBA Code:
' Sorted list Info: http://www.snb-vba.eu/VBA_Sortedlist_en.html
' Tools-->references to mscorlib.dll may be required

' Program makes a master list of all numbers
' Then it subsets the low numbers if the number exists in the master list and finds a random number
' next it removes the number just found from the master list and repeats the process for high numbers
' In case a working number set is not found the Do Until repeats as necessary

Option Explicit

Sub GetNumbers()

    Dim slMast  As Object
    Dim slTemp  As Object
    Dim i       As Long
    Dim j       As Long
    Dim Ans     As Long
    Dim loMin   As Long
    Dim loMax   As Long
    Dim hiMin   As Long
    Dim noError As Boolean
    Dim ws      As Worksheet
    
    Set ws = Worksheets("Sheet1")
    ws.Range("C1:C20").ClearContents
    
    Set slMast = CreateObject("System.Collections.SortedList")
    Set slTemp = CreateObject("System.Collections.SortedList")
    
    
    Do Until noError
        ' Populate Master List
        slMast.Clear
        For i = 10 To 29
            slMast.Add Format(i, "00"), i
        Next
        
        noError = True
        For j = 1 To 10
        
            ' Fill Temp List from Master with HIGH Numbers
            hiMin = WorksheetFunction.Max(10, 22 - j)
            slTemp.Clear
            For i = hiMin To 29
                If slMast.Item(Format(i, "00")) > 0 Then
                    slTemp.Add Format(Rnd(), "0.0000000") & Format(i, "00"), i
                End If
            Next i
        
            ' Number found - output and remove
            If slTemp.Count > 0 Then
                Ans = slTemp.Getbyindex(0)
            Else
                noError = False
            End If

            ws.Cells(hiMin - 1, 3) = Ans
            slMast.Remove Format(Ans, "00")
            
            ' Fill Temp List from Master with LOW Numbers
            loMin = WorksheetFunction.Max(10, j + 1)
            loMax = WorksheetFunction.Min(j + 20, 29)
            slTemp.Clear
            For i = loMin To loMax
                If slMast.Item(Format(i, "00")) > 0 Then
                    slTemp.Add Format(Rnd(), "0.0000000") & Format(i, "00"), i
                End If
            Next i
        
            ' Number found - output and remove
            If slTemp.Count > 0 Then
                Ans = slTemp.Getbyindex(0)
            Else
                noError = False
            End If
            ws.Cells(j, 3) = Ans
            slMast.Remove Format(Ans, "00")
  
        Next j

    Loop

End Sub
 
Upvote 0
Try this
Book1
ABCDEFGHIJKLM
111011
221117
331210
441323
551416
661518
771614
881722
991813
10101924
11112012
12122129
13132219
14142315
15152427
16162521
17172625
18182726
19192820
20202928
Sheet1
Cell Formulas
RangeFormula
C1C1=RANDBETWEEN(10,29)
C2:C20C2=IF(AGGREGATE(15,6,$B$1:$B$20/(COUNTIF($C$1:C1,$B$1:$B$20)=0),1)=A2+1,A2+1,AGGREGATE(15,6,$B$1:$B$20/(COUNTIF($C$1:C1,$B$1:$B$20)=0)/($B$1:$B$20>A2)/($B$1:$B$20<A2+20),RANDBETWEEN(1,COUNT($B$1:$B$20/(COUNTIF($C$1:C1,$B$1:$B$20)=0)/($B$1:$B$20>A2)/($B$1:$B$20<A2+20)))))
Press CTRL+SHIFT+ENTER to enter array formulas.
 
Upvote 0
Hi,

You will need to create a new Module and paste in the code below.
You may need to go into Tools->References to include mscorlib.dll but it worked for me without having to do that.
There is an issue in that that the numbers for rows 1 to 9 can be chosen without using the number 10 (or 1-10 without 11 etc) but 10 cannot be used elsewhere. If an invalid set of numbers is found the code loops until a valid set is discovered. It usually only takes one iteration but it has taken 8 on a couple of occasions.

The numbers are chosen for the top and bottom rows first and then it works towards the middle.



VBA Code:
' Sorted list Info: http://www.snb-vba.eu/VBA_Sortedlist_en.html
' Tools-->references to mscorlib.dll may be required

' Program makes a master list of all numbers
' Then it subsets the low numbers if the number exists in the master list and finds a random number
' next it removes the number just found from the master list and repeats the process for high numbers
' In case a working number set is not found the Do Until repeats as necessary

Option Explicit

Sub GetNumbers()

    Dim slMast  As Object
    Dim slTemp  As Object
    Dim i       As Long
    Dim j       As Long
    Dim Ans     As Long
    Dim loMin   As Long
    Dim loMax   As Long
    Dim hiMin   As Long
    Dim noError As Boolean
    Dim ws      As Worksheet
   
    Set ws = Worksheets("Sheet1")
    ws.Range("C1:C20").ClearContents
   
    Set slMast = CreateObject("System.Collections.SortedList")
    Set slTemp = CreateObject("System.Collections.SortedList")
   
   
    Do Until noError
        ' Populate Master List
        slMast.Clear
        For i = 10 To 29
            slMast.Add Format(i, "00"), i
        Next
       
        noError = True
        For j = 1 To 10
       
            ' Fill Temp List from Master with HIGH Numbers
            hiMin = WorksheetFunction.Max(10, 22 - j)
            slTemp.Clear
            For i = hiMin To 29
                If slMast.Item(Format(i, "00")) > 0 Then
                    slTemp.Add Format(Rnd(), "0.0000000") & Format(i, "00"), i
                End If
            Next i
       
            ' Number found - output and remove
            If slTemp.Count > 0 Then
                Ans = slTemp.Getbyindex(0)
            Else
                noError = False
            End If

            ws.Cells(hiMin - 1, 3) = Ans
            slMast.Remove Format(Ans, "00")
           
            ' Fill Temp List from Master with LOW Numbers
            loMin = WorksheetFunction.Max(10, j + 1)
            loMax = WorksheetFunction.Min(j + 20, 29)
            slTemp.Clear
            For i = loMin To loMax
                If slMast.Item(Format(i, "00")) > 0 Then
                    slTemp.Add Format(Rnd(), "0.0000000") & Format(i, "00"), i
                End If
            Next i
       
            ' Number found - output and remove
            If slTemp.Count > 0 Then
                Ans = slTemp.Getbyindex(0)
            Else
                noError = False
            End If
            ws.Cells(j, 3) = Ans
            slMast.Remove Format(Ans, "00")
 
        Next j

    Loop

End Sub
Thank you for this, it works great but i dont understand VBA enough to amend this as the numbers & criteria change.
 
Upvote 0
Try this
Book1
ABCDEFGHIJKLM
111011
221117
331210
441323
551416
661518
771614
881722
991813
10101924
11112012
12122129
13132219
14142315
15152427
16162521
17172625
18182726
19192820
20202928
Sheet1
Cell Formulas
RangeFormula
C1C1=RANDBETWEEN(10,29)
C2:C20C2=IF(AGGREGATE(15,6,$B$1:$B$20/(COUNTIF($C$1:C1,$B$1:$B$20)=0),1)=A2+1,A2+1,AGGREGATE(15,6,$B$1:$B$20/(COUNTIF($C$1:C1,$B$1:$B$20)=0)/($B$1:$B$20>A2)/($B$1:$B$20<A2+20),RANDBETWEEN(1,COUNT($B$1:$B$20/(COUNTIF($C$1:C1,$B$1:$B$20)=0)/($B$1:$B$20>A2)/($B$1:$B$20<A2+20)))))
Press CTRL+SHIFT+ENTER to enter array formulas.
Try this
Book1
ABCDEFGHIJKLM
111011
221117
331210
441323
551416
661518
771614
881722
991813
10101924
11112012
12122129
13132219
14142315
15152427
16162521
17172625
18182726
19192820
20202928
Sheet1
Cell Formulas
RangeFormula
C1C1=RANDBETWEEN(10,29)
C2:C20C2=IF(AGGREGATE(15,6,$B$1:$B$20/(COUNTIF($C$1:C1,$B$1:$B$20)=0),1)=A2+1,A2+1,AGGREGATE(15,6,$B$1:$B$20/(COUNTIF($C$1:C1,$B$1:$B$20)=0)/($B$1:$B$20>A2)/($B$1:$B$20<A2+20),RANDBETWEEN(1,COUNT($B$1:$B$20/(COUNTIF($C$1:C1,$B$1:$B$20)=0)/($B$1:$B$20>A2)/($B$1:$B$20<A2+20)))))
Press CTRL+SHIFT+ENTER to enter array formulas.
Hi, this solution is perfect & i really appreciate the time taken to do this, as i have managed to help resolve this problem, however.... i was wondering if this can be adjusted for the following dilemma.
While appreciate this wasn't part of the initial request, when i have changed the input numbers, i get the #NUM error, i understand that this is because it wont duplicate numbers & runs out of an answer at 15, can the formula be amended to suit the multiple numbers?
Number Shuffle Solution.xlsm
ABCDEFGH
11319
21415
3253
4255
52610
62716
72814
82929
9394
1041020
114109
124106
1341021
1451013
1551224
1651223
1751212
186127
1961327
2061333
2161432
227148
2371525
2471522
2571518
2681528
27916#NUM!
28918#NUM!
291019#NUM!
301019#NUM!
311120#NUM!
321220#NUM!
331220#NUM!
341320#NUM!
351421#NUM!
361421#NUM!
371522#NUM!
381623#NUM!
391623#NUM!
401624#NUM!
411625#NUM!
421627#NUM!
431828#NUM!
441929#NUM!
452132#NUM!
462533#NUM!
472533#NUM!
48
Sheet2 (2)
Cell Formulas
RangeFormula
C1C1=RANDBETWEEN(10,20)
C2:C47C2=IF(AGGREGATE(15,6,$B$1:$B$47/(COUNTIF($C$1:C1,$B$1:$B$47)=0),1)=A2+1,A2+1,AGGREGATE(15,6,$B$1:$B$47/(COUNTIF($C$1:C1,$B$1:$B$47)=0)/($B$1:$B$47>A2)/($B$1:$B$47<A2+$I$1),RANDBETWEEN(1,COUNT($B$1:$B$47/(COUNTIF($C$1:C1,$B$1:$B$47)=0)/($B$1:$B$47>A2)/($B$1:$B$47<A2+$I$1)))))
Press CTRL+SHIFT+ENTER to enter array formulas.
Cells with Conditional Formatting
CellConditionCell FormatStop If True
C1:C47Cell ValueduplicatestextNO
C1:C47Cell ValueduplicatestextNO
 
Upvote 0
Number 5 has 2 times in column B, do you want it to be 2 times in the result column?
 
Upvote 0
Number 5 has 2 times in column B, do you want it to be 2 times in the result column?
Hi, yes there are duplicates in each column, essentially these are the Euro lottery numbers drawn over a 47 week period i.e. the first & second numbers on a lottery ticket sorted low to high, so yes i need all the duplicate numbers i.e. 5 two times, 10 five times etc..
 
Upvote 0

Forum statistics

Threads
1,215,059
Messages
6,122,918
Members
449,094
Latest member
teemeren

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