P
Patrick Molloy
1) get the cell values (2) randomise (3) sort (4) replace
Sub RandomSelectetion()
Dim Values() As Variant ' tpo hold the values
' and the random numbers
Dim index As Long ' for/next counter
Dim index2 As Long ' for sorting
Dim temp As String ' for sorting
Dim Target As Range
Dim cell As Range
Randomize Timer ' re-initialise the randomiser
Set Target = Selection
ReDim Values(1 To Target.Count, 1 To 2)
' get the selected cell values and assign a RND number
index = 0
For Each cell In Target
index = index + 1
Values(index, 1) = cell.Value
Values(index, 2) = Rnd
Next
'simple sort
For index = 1 To Target.Count - 1
For index2 = index + 1 To Target.Count
If Values(index, 2) > Values(index2, 2) Then
temp = Values(index, 1)
Values(index, 1) = Values(index2, 1)
Values(index2, 1) = temp
End If
Next
Next
' push back to spreadsheet
index = 0
For Each cell In Target
index = index + 1
cell.Value = Values(index, 1)
Next
End Sub
Sub RandomSelectetion()
Dim Values() As Variant ' tpo hold the values
' and the random numbers
Dim index As Long ' for/next counter
Dim index2 As Long ' for sorting
Dim temp As String ' for sorting
Dim Target As Range
Dim cell As Range
Randomize Timer ' re-initialise the randomiser
Set Target = Selection
ReDim Values(1 To Target.Count, 1 To 2)
' get the selected cell values and assign a RND number
index = 0
For Each cell In Target
index = index + 1
Values(index, 1) = cell.Value
Values(index, 2) = Rnd
Next
'simple sort
For index = 1 To Target.Count - 1
For index2 = index + 1 To Target.Count
If Values(index, 2) > Values(index2, 2) Then
temp = Values(index, 1)
Values(index, 1) = Values(index2, 1)
Values(index2, 1) = temp
End If
Next
Next
' push back to spreadsheet
index = 0
For Each cell In Target
index = index + 1
cell.Value = Values(index, 1)
Next
End Sub