Randomly Distribution of items across a range with vba

kelly mort

Well-known Member
Joined
Apr 10, 2017
Messages
2,169
Office Version
  1. 2016
Platform
  1. Windows
I have this table of the range

B2:J6

And I am looking for a way to randomly distribute the letters A-I across.

These are the rules:
1. A row can take just A or A and A . If 2 As, then they should be immediately adjacent each other. Example B2C2.

2. The first four letters can appear more than the last five.

3. Columns E and H are excluded.

I hope this is achievable.

Thanks in advance
 
No suggestion just a another version of the table in post number 8 as it doesn't paste well in Excel.

Excel 2010
ABCDEFGHIJ
1BBGGICC
2AAEDDFF
3IIGAACC
4HCCBHDD
5BDHEEFA

<colgroup><col style="width: 25pxpx"><col><col><col><col><col><col><col><col><col><col></colgroup><thead>
</thead><tbody>
</tbody>
Sheet1

Okay thanks for the help with the table for me.
 
Upvote 0

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
PS as you only have 9 letters in your universe, it would be simpler to treat these as the digits 1 through 9 and then later translate the range into letters, fruits or anything your heart desires.


Whatever works cool is okay for me.
 
Upvote 0
This is the macro


Code:
Sub Randomly()
    Dim r As Range, c As Range, ale As Long, letter As String, exists As Boolean, cnt As Long


    Set r = Range("B2:D6,F2:G6,I2:J6")
    r.ClearContents
    For Each c In r
        exists = True
        Do While exists
            ale = WorksheetFunction.RandBetween(65, 73)
            letter = Chr(ale)
            exists = False
            cnt = WorksheetFunction.CountIf(Range(Cells(c.Row, "B"), Cells(c.Row, "J")), letter)
            If (cnt = 1 And c.Offset(, -1).Value <> letter) Or cnt = 2 Then exists = True
        Loop
        c.Value = letter
    Next
End Sub

The result will be like this:

<table border="1" cellspacing="0" style="font-family:Calibri,Arial; font-size:11pt; background-color:#ffffff; "> <colgroup><col style="font-weight:bold; width:30px; " /><col style="width:57.98px;" /><col style="width:57.98px;" /><col style="width:57.98px;" /><col style="width:57.98px;" /><col style="width:57.98px;" /><col style="width:57.98px;" /><col style="width:57.98px;" /><col style="width:57.98px;" /><col style="width:57.98px;" /></colgroup><tr style="background-color:#cacaca; text-align:center; font-weight:bold; font-size:8pt; "><td > </td><td >B</td><td >C</td><td >D</td><td >E</td><td >F</td><td >G</td><td >H</td><td >I</td><td >J</td></tr><tr style="height:32px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >2</td><td style="text-align:center; ">D</td><td style="text-align:center; ">B</td><td style="text-align:center; ">F</td><td style="background-color:#d8d8d8; "> </td><td style="text-align:center; ">C</td><td style="text-align:center; ">E</td><td style="background-color:#d8d8d8; "> </td><td style="text-align:center; ">H</td><td style="text-align:center; ">G</td></tr><tr style="height:32px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >3</td><td style="text-align:center; ">B</td><td style="text-align:center; ">C</td><td style="text-align:center; ">C</td><td style="background-color:#d8d8d8; "> </td><td style="text-align:center; ">F</td><td style="text-align:center; ">A</td><td style="background-color:#d8d8d8; "> </td><td style="text-align:center; ">I</td><td style="text-align:center; ">I</td></tr><tr style="height:32px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >4</td><td style="text-align:center; ">G</td><td style="text-align:center; ">G</td><td style="text-align:center; ">I</td><td style="background-color:#d8d8d8; "> </td><td style="text-align:center; ">B</td><td style="text-align:center; ">H</td><td style="background-color:#d8d8d8; "> </td><td style="text-align:center; ">C</td><td style="text-align:center; ">A</td></tr><tr style="height:32px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >5</td><td style="text-align:center; ">I</td><td style="text-align:center; ">C</td><td style="text-align:center; ">D</td><td style="background-color:#d8d8d8; "> </td><td style="text-align:center; ">F</td><td style="text-align:center; ">G</td><td style="background-color:#d8d8d8; "> </td><td style="text-align:center; ">B</td><td style="text-align:center; ">B</td></tr><tr style="height:32px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >6</td><td style="text-align:center; ">E</td><td style="text-align:center; ">I</td><td style="text-align:center; ">G</td><td style="background-color:#d8d8d8; "> </td><td style="text-align:center; ">H</td><td style="text-align:center; ">D</td><td style="background-color:#d8d8d8; "> </td><td style="text-align:center; ">B</td><td style="text-align:center; ">F</td></tr></table>

But it only works with the letters from A to I.
If you want to put fruits or things, you must complete the code to change the letters for fruits. Or add a fruit array in the macro. There are several alternatives, but for now it works with letters.
 
Upvote 0
Okay very cool!!!


I found a way to replace the the letters with the fruits and it's working cute since I am not good at the arrays yet.

So I wanna know if it is possible to make sure A and B are always more than the others.

Don't worry if that's not cool .

Have a wonderful time.
 
Last edited:
Upvote 0
What you can do when you substitute your fruits for letters is sort the letters by number of occurrences and then just make sure your two largest occurrences are the ones you want the most of. No one needs to know that behind the scenes you are changing the representation of the random letters. Create a table that counts the 9 letters. Then create a new table that has the fruit names replacing the letters based on their counts.
 
Upvote 0
What you can do when you substitute your fruits for letters is sort the letters by number of occurrences and then just make sure your two largest occurrences are the ones you want the most of. No one needs to know that behind the scenes you are changing the representation of the random letters. Create a table that counts the 9 letters. Then create a new table that has the fruit names replacing the letters based on their counts.


Okay thanks.

I appreciate the assistance
 
Upvote 0
Excel 2016 (Windows) 32 bit
B
C
D
E
F
G
H
I
J
2
DBECCAI
3
BFDIIGC
4
AEDBHGF
5
CFHDIGE
6
IFGCBDD
7
8
9
D
6​
Apple
10
C
5​
Orange
11
I
5​
Mango
12
B
4​
Lime
13
F
4​
Lemon
14
G
4​
Grape
15
E
3​
Cherry
16
A
2​
Banana
17
H
2​
Peach
18
19
20
AppleLimeCherryOrangeOrangeBananaMango
21
LimeLemonAppleMangoMangoGrapeOrange
22
BananaCherryAppleLimePeachGrapeLemon
23
OrangeLemonPeachAppleMangoGrapeCherry
24
MangoLemonGrapeOrangeLimeAppleApple
Sheet: Sheet4
 
Upvote 0
Excel 2016 (Windows) 32 bit
B
C
D
E
F
G
H
I
J
2
EEGDFAH
3
IADCGHH
4
BHFGDCA
5
EFIBGHH
6
EBHFAGG
7
8
Sort byCountFixed list
9
H
7​
Apple
10
G
6​
Orange
11
F
4​
Mango
12
E
4​
Lime
13
A
4​
Lemon
14
D
3​
Grape
15
B
3​
Cherry
16
C
2​
Banana
17
I
2​
Peach
18
19
20
LimeLimeOrangeGrapeMangoLemonApple
21
PeachLemonGrapeBananaOrangeAppleApple
22
CherryAppleMangoOrangeGrapeBananaLemon
23
LimeMangoPeachCherryOrangeAppleApple
24
LimeCherryAppleMangoLemonOrangeOrange
25
26
27
=INDEX($E$9:$E$17,MATCH(B2,$B$9:$B$17,0),1)
Sheet: Sheet4
 
Upvote 0
Okay very cool!!!
I found a way to replace the the letters with the fruits and it's working cute since I am not good at the arrays yet.
So I wanna know if it is possible to make sure A and B are always more than the others.
Don't worry if that's not cool .
Have a wonderful time.


Macro updated with fruits or whatever you need:

Code:
Sub Randomly()
    Dim r As Range, c As Range, ale As Long, letter As String, exists As Boolean, cnt As Long
    Dim things As Variant
    things = Array("Apple", "Orange", "Mango", "Lime", "Lemon", "Mango", "Melon", "Pine", "Pear")
    
    Set r = Range("B2:D6,F2:G6,I2:J6")
    r.ClearContents
    For Each c In r
        exists = True
        Do While exists
            ale = WorksheetFunction.RandBetween(0, 8)
            'letter = Chr(ale)
            letter = things(ale)
            exists = False
            cnt = WorksheetFunction.CountIf(Range(Cells(c.Row, "B"), Cells(c.Row, "J")), letter)
            If (cnt = 1 And c.Offset(, -1).Value <> letter) Or cnt = 2 Then exists = True
        Loop
        c.Value = letter
    Next
End Sub

So I wanna know if it is possible to make sure A and B are always more than the others.

A and B or A, B, C and D?
Appear more in a row or in the whole matrix?
 
Upvote 0

Forum statistics

Threads
1,214,786
Messages
6,121,546
Members
449,038
Latest member
Guest1337

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