While I haven't dug into your code, your earlier comment about looping through the labels got me thinking that its not necessary.
It can be arranged that the labels are fixed with lbl0, lbl1, lbl2 and lbl3 in the turntable.
An array NumberOrder(0 to 19) initially holds the values 1,2,3,...,20
A value Pointer indicates which member of the array is in lbl0, so that
lbl0.Caption = NumberOrder(Pointer)
lbl1.Caption = NumberOrder((Pointer + 1) Mod 20)
lbl2.Caption = NumberOrder((Pointer + 2) Mod 20)
Me.Controls("lbl" & i).Caption = NumberOrder((Pointer + i) Mod 20)
One rotates the track by changing Pointer.
On turns the turntable by swapping NumberOrder(Pointer) with NumberOrder(Pointer + 3) and NumberOrder(Pointer+1) with NumberOrder(Pointer+2).
Randomizing is done by rotating a random number of squares and then turning the turntable, 1000 times.
Sub RandomNumberOrder()
Dim i As Long
For i = 1 To 1000
Pointer = WorksheetFunction.RandBetween(0, 19)
TurnTheTable
Next i
End Sub
And then I got into it and produced the attached workbook.
The size of the rotation is entered into the text box, if a number <1 or >19 is entered, it changes to the DefaultRotationValue that is set by the calling code.
With UserForm1
.DefaultRotationValue = 4
.Show
End With
In the attached, the labels are created and positioned by the MakeTrackLabels routine.
PostionControls is called in the Activate event because the .InsideHeight property is unreliable in the Intialize event.
This will alert the user when they have completed the puzzle. Note that 20,19,18,...,3,2,1 counts as victory as well as 1,2,3...18,19,10 and that the 1 doesn't need to be in any particular position. Also note that these conditions mean that victory will never be achieved after a Rotation, only after a TurnTheTable.
Bookmarks