Re: Random shuffling of data in rows
Hello,
Try this spreadsheet or copy the code below into your code. If you look in the VBA editor at the tree view on the left hand side it shows all the worksheets, modules, classes, userforms for your project. If you select 'Workbook' from the tree view and paste the code there it will run on workbook open or you could just use the attached file. Fixed the loop went one too many times for the samples and the numbering now starts at 1 instead of 0 for both participants and samples.
[ATTACH=CONFIG]54390[/ATTACH]
Private Sub Workbook_Open()
Dim intCounter As Integer, intSamplesCount As Integer, intParticipantCount As Integer, intCounter1 As Integer, intCounter2 As Integer, intTmp As Integer
Dim ParticipantCount As Variant, SampleCount As Variant
Dim strSamples() As String, strTmp() As String
Dim oSheet As Worksheet, oSheet1 As Worksheet
'Participant Count
ParticipantCount = Application.InputBox("Number of Participants", "Participant Count", 30)
If ParticipantCount = vbNullString Or ParticipantCount = False Then Exit Sub
intParticipantCount = ParticipantCount
'Sample Count
SampleCount = Application.InputBox("Number of Samples", "Sample Count", 12)
If SampleCount = vbNullString Or SampleCount = False Then Exit Sub
intSamplesCount = SampleCount
ReDim strSamples(intSamplesCount - 1)
ReDim strTmp(intSamplesCount - 1)
Set oSheet = Sheets("Sheet1")
Set oSheet1 = Sheets("Sheet2")
oSheet.Cells.ClearContents
oSheet1.Cells.ClearContents
For intCounter = 0 To intSamplesCount - 1
strSamples(intCounter) = Int((999 - 100 + 1) * Rnd + 100)
oSheet.Cells(1, intCounter + 2) = "S" & intCounter + 1
oSheet1.Cells(1, intCounter + 2) = "S" & intCounter + 1
Next
For intCounter = 0 To intParticipantCount - 1
oSheet.Cells(intCounter + 2, 1) = "P" & intCounter + 1
oSheet1.Cells(intCounter + 2, 1) = "P" & intCounter + 1
'Reset Temp Array Element
For intCounter1 = 0 To intSamplesCount - 1
strTmp(intCounter1) = ""
Next
For intCounter1 = 0 To intSamplesCount - 1
intTmp = Int(((intSamplesCount - 1) + 1) * Rnd)
strTmp(intCounter1) = strSamples(intTmp)
intCounter2 = -1
Do While intCounter2 < intCounter1 - 1
intCounter2 = intCounter2 + 1
If strTmp(intCounter2) = strTmp(intCounter1) Then
intCounter2 = -1
intTmp = Int(((intSamplesCount - 1) + 1) * Rnd)
strTmp(intCounter1) = strSamples(intTmp)
End If
Loop
oSheet.Cells(intCounter + 2, intCounter1 + 2) = strTmp(intCounter1)
oSheet1.Cells(intCounter + 2, intCounter1 + 2) = intTmp
Next
Next
End Sub
Display More