Running a Macro Until result Achieved

PM1

Board Regular
Joined
Oct 28, 2005
Messages
192
I have a table shown A2:F18 below that Randomises each of the 4 columns A-D with No's from 1 - 17by pressing F9 - this is done in a the following formula:-

=LARGE(ROW($1:$18)*NOT(COUNTIF(A$1:A1, ROW($1:$18))), RANDBETWEEN(1,18-ROW(A1)))

I have another column F which validates if the row is unique and has no duplicates and shows 1 or 0 and has another long winded formula:-

=IF(A2=B2,0,IF(A2=C2,0,IF(A2=D2,0,IF(B2=C2,0,IF(B2=D2,0,IF(C2=D2,0,1))))))

At the bottom of the table I have a cell that checks if column F = 17, being all unique rows and reports NOT YET or SUCCESS, SUCCESS has not yet been reached and my F9 finger is sore

I need to be able to run this in a Macro until the result is = 17, any ideas?

Excel Workbook
ABCDEFGHI
273415*1***
317181413*1***
411882*0***
5611177*1***
61212185*0***
736212*1***
849139*0***
91810716*1***
101321111*0***
11151356*1***
1285128*0***
1314141514*0***
1457163*1***
15917618*1***
16241017*1***
171615310*1***
18101694*1***
19********NOT YET
Sheet1
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Code:
do until range("I19")= 17
    application.calculate
loop

this will keep going forever if you aren't careful...
 
Upvote 0
Still running :(

Surely the randomisation formula cannot be wrong?, it's giving me numbers from 1 - 17 in 4 different columns. Whats the permutation on this?.

Now this is annoying...............
 
Upvote 0
To stop, hold [escape] until code breaks

17 x 17 x 17 x 17 = 83,521 possible scenarios, assumin you only look at an "ordered" list. Randomising things might lead to more or less before a result is found

It might help if you ensure you only generate numbers that have not been used in the same loop, i.e. prevent duplication before you write the values into your table?
 
Upvote 0
ok, so let me get this straight, you are simply trying to generate 17 sets of unique numbers taken randomly from the set 1-17?

Your problem in what you have done so far is that you are trying to get every combination to be unique at the same time, and if not you are generating another set of data. Do them 1 at a time, then move on when each is ok
 
Upvote 0
"baitmaster" you are correct. I know I can do 1 - 17 and step down 1 in each column but to me that is not random.
 
Upvote 0
so long as you produce a line of random numbers for each set of results, whether you produce them all in one hit, or 1 row at a time, surely each line is still random and hence randomness ensues throughout?

Or am I missing something in the complexity of your formula? Does a certain type of result influence any other line? I don't believe it should, and hence I think my approach remains ok

I believe you can simply create a line of random numbers, where each number is unique, then generate another line of random numbers etc. The only thing I see that prevents true randomness is that the numbers cannot be duplicated

Im off home in a minute, but I have a random number generator there that continually produces sequences of unique random numbers using VBA. I'll post the code later if I find it
 
Upvote 0
If only it was that simple. I have arranged each column into 3 groups of 3 and 2 groups of 4.

My idea is to achieve a state where each group is unique in that at no time does any group match any other, I will then achieve 12 groups of 3 and 8 groups of 4 all unique.

it is similar in a way to http://en.wikipedia.org/wiki/Nurse_scheduling_problem in that there is a lot of difficulties encountered in its constraint.
 
Upvote 0
OK...

here is a piece of code that I just wrote, that will meet the needs I described in my previous post

Code:
Sub randomDataNoDuplication()
Dim i As Integer, j As Integer, thisRand As Integer
With Range("randoms")
    For i = 1 To .Rows.Count
        For j = 1 To .Columns.Count
repeatRand:
            thisRand = WorksheetFunction.RoundUp(Rnd() * 17, 0)
            .Cells(i, j) = thisRand
            If WorksheetFunction.CountIf(.Rows(i), thisRand) > 1 Then GoTo repeatRand
        Next j
    Next i
End With
End Sub

To make it work, you need to name your output section "randoms"

It doesn't quite manage what you want given your new criteria, but shows how you could structure some VBA to do this, In effect I'm looping through and generating new randoms, then regenerating them if duplication is found. You could probably add an additional test that regenerates the entire line when needed. I'll think about how to go about this next...
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,287
Members
452,902
Latest member
Knuddeluff

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