I beg to differ.
The choices with that code are not truly random. For example if the first number chosen in a column happens to be, say, row 16 in the worksheet then it is then impossible for the number from row 6 in the sheet to also be chosen for that column. The error is with the InStr line in the code.
As another les important point:
You will likely never actually notice but the code is also somewhat inefficient in its choosing of the 'next' random value for r since it picks a value and then checks (sometimes incorrectly as I pointed out above) to see if that number has already been chosen. If it has it repeats that process until a suitable random value is chosen. In theory it could get caught up picking an already used value 5 or 10 or 10000 times before choosing a suitable one.
To demonstrate my first point above I altered the suggested code to ensure that row 16 was chosen first in every column as follows
I then ran the code several times without clearing the yellow colour and this is the result. You see that row 6 has never been chosen
Goalexcel.xlsm |
---|
|
---|
| A | B | C | D | E | F | G | H | I | J |
---|
1 | Sect 1 | Sect 2 | Sect 3 | Sect 4 | Sect 5 | Sect 6 | Sect 7 | Sect 8 | Sect 9 | Sect 10 |
---|
2 | 1101 | 1117 | 1133 | 1149 | 1165 | 1181 | 1197 | 1213 | 1229 | 1245 |
---|
3 | 1102 | 1118 | 1134 | 1150 | 1166 | 1182 | 1198 | 1214 | 1230 | 1246 |
---|
4 | 1103 | 1119 | 1135 | 1151 | 1167 | 1183 | 1199 | 1215 | 1231 | 1247 |
---|
5 | 1104 | 1120 | 1136 | 1152 | 1168 | 1184 | 1200 | 1216 | 1232 | 1248 |
---|
6 | 1105 | 1121 | 1137 | 1153 | 1169 | 1185 | 1201 | 1217 | 1233 | 1249 |
---|
7 | 1106 | 1122 | 1138 | 1154 | 1170 | 1186 | 1202 | 1218 | 1234 | 1250 |
---|
8 | 1107 | 1123 | 1139 | 1155 | 1171 | 1187 | 1203 | 1219 | 1235 | 1251 |
---|
9 | 1108 | 1124 | 1140 | 1156 | 1172 | 1188 | 1204 | 1220 | 1236 | 1252 |
---|
10 | 1109 | 1125 | 1141 | 1157 | 1173 | 1189 | 1205 | 1221 | 1237 | 1253 |
---|
11 | 1110 | 1126 | 1142 | 1158 | 1174 | 1190 | 1206 | 1222 | 1238 | 1254 |
---|
12 | 1111 | 1127 | 1143 | 1159 | 1175 | 1191 | 1207 | 1223 | 1239 | 1255 |
---|
13 | 1112 | 1128 | 1144 | 1160 | 1176 | 1192 | 1208 | 1224 | 1240 | 1256 |
---|
14 | 1113 | 1129 | 1145 | 1161 | 1177 | 1193 | 1209 | 1225 | 1241 | 1257 |
---|
15 | 1114 | 1130 | 1146 | 1162 | 1178 | 1194 | 1210 | 1226 | 1242 | 1258 |
---|
16 | 1115 | 1131 | 1147 | 1163 | 1179 | 1195 | 1211 | 1227 | 1243 | 1259 |
---|
17 | 1116 | 1132 | 1148 | 1164 | 1180 | 1196 | 1212 | 1228 | 1244 | 1260 |
---|
18 | | | | | | | | | | |
---|
19 | | | | | | | | | | |
---|
20 | Sect 1 | Sect 2 | Sect 3 | Sect 4 | Sect 5 | Sect 6 | Sect 7 | Sect 8 | Sect 9 | Sect 10 |
---|
21 | 1115 | 1131 | 1147 | 1163 | 1179 | 1195 | 1211 | 1227 | 1243 | 1259 |
---|
22 | 1108 | 1122 | 1142 | 1158 | 1165 | 1190 | 1203 | 1225 | 1244 | 1245 |
---|
23 | 1106 | 1132 | 1135 | 1156 | 1175 | 1191 | 1197 | 1221 | 1236 | 1251 |
---|
|
---|
Here is an alternative code that addresses both of the above issues.
It also gives you an easy way to change if you wanted to pick other than 3 numbers from each column.
My code does assume that all the numbers in any single column are different as is the case with your sample.
VBA Code:
Sub Pick_N()
Dim d As Object
Dim a As Variant, b As Variant
Dim c As Long, i As Long, k As Long, uba As Long
Const PickHowMany As Long = 3 '<- Edit to suit
Randomize
Application.ScreenUpdating = False
Set d = CreateObject("Scripting.Dictionary")
With Range("A2:T17")
a = .Value
uba = UBound(a)
ReDim b(1 To PickHowMany, 1 To UBound(a, 2))
For c = 1 To UBound(a, 2)
d.RemoveAll
For i = 1 To uba
d(a(i, c)) = i
Next i
For i = 1 To PickHowMany
k = 1 + Int(Rnd() * d.Count)
b(i, c) = d.Keys()(k - 1)
.Cells(d.Items()(k - 1), c).Interior.Color = vbYellow
d.Remove b(i, c)
Next i
Next c
Range("A20").Resize(, UBound(a, 2)).Value = .Rows(0).Value
Range("A21").Resize(UBound(b), UBound(b, 2)).Value = b
End With
Application.ScreenUpdating = True
End Sub