Help (Please!),
I'm trying to randomly generate thirteen "Distractors" (single-digit numerals ranging from 2 to 8).
However, the randomization must be subject to the condition that each new Distractor ("i") has not appeared as one of the previous three distractors (that is "i - 1", "i - 2", "i - 3")
Also, it would be ideal if all 7 digits were used once before they started recycling, though this isn't critical and I haven't even started on it in the code below.
Here's my code- I'm certainly not married to it. Any help is appreciated.
-- Greg
* I'm working on Windows XP (code must also work on Windows 7 eventually) and Excel 2007
I'm trying to randomly generate thirteen "Distractors" (single-digit numerals ranging from 2 to 8).
However, the randomization must be subject to the condition that each new Distractor ("i") has not appeared as one of the previous three distractors (that is "i - 1", "i - 2", "i - 3")
Also, it would be ideal if all 7 digits were used once before they started recycling, though this isn't critical and I haven't even started on it in the code below.
Here's my code- I'm certainly not married to it. Any help is appreciated.
-- Greg
* I'm working on Windows XP (code must also work on Windows 7 eventually) and Excel 2007
Code:
Sub Random_Distractor()
'
' Random_Distractor Macro
'
'
Dim i As Integer
Dim Cellname, PrevCellname, PrevTwoCellname, PrevThreeCellname As String
For i = 1 To 13
Cellname = "E" & "i"
Range("Cellname").Activate
ActiveCell.FormulaR1C1 = "=RANDBETWEEN(2,8)"
PrevCellname = "E" & "(i - 1)"
PrevTwoCellname = "E" & "(i - 2)"
PrevThreeCellname = "E" & "(i - 3)"
If i > 1 Then
Do
If ActiveCell.Value = Range(PrevCellname).Value Then
ActiveCell.FormulaR1C1 = "=RandBetween(2,8)"
End If
Loop Until ActiveCell.Value <> Range(PrevCellname).Value
End If
If i > 2 Then
Do
If ActiveCell.Value = Range(PrevCellname).Value Or ActiveCell.Value = Range(PrevTwoCellname).Value Then
ActiveCell.FormulaR1C1 = "=RandBetween(2,8)"
End If
Loop Until ActiveCell.Value <> Range(PrevCellname).Value And ActiveCell.Value <> Range(PrevTwoCellname).Value
End If
If i > 3 Then
Do
If ActiveCell.Value = Range(PrevCellname).Value Or ActiveCell.Value = Range(PrevTwoCellname).Value Or ActiveCell.Value = Range(PrevThreeCellname).Value Then
ActiveCell.FormulaR1C1 = "=RandBetween(2,8)"
End If
Loop Until ActiveCell.Value <> Range(PrevCellname).Value And ActiveCell.Value <> Range(PrevTwoCellname).Value And ActiveCell.Value <> Range(PrevThreeCellname).Value
End If
Next i
Range(Cellname).Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End Sub