View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.worksheet.functions
Harlan Grove[_2_] Harlan Grove[_2_] is offline
external usenet poster
 
Posts: 1,231
Default Random Sort selected cells

Guy wrote...
Does anyone have a VBA code that will randomly sort a set of 'Selected' cells
in a column when a forms object assigned to that code is selected?


You mean shuffle the cell values in a range? Maybe something like


Sub foo()
Const FOOBAR As Long = 4

Dim v As Variant, t As Variant
Dim i1 As Long, i2 As Long, j1 As Long, j2 As Long
Dim k As Long, m As Long, n As Long

If Not TypeOf Selection Is Range Then Exit Sub
v = Selection.Areas(1).Value2
k = Selection.Areas(1).Rows.Count
m = Selection.Areas(1).Columns.Count
n = FOOBAR * k * m

Do While n 0
n = n - 1
i1 = Int(1 + k * Rnd)
i2 = Int(1 + k * Rnd)
j1 = Int(1 + m * Rnd)
j2 = Int(1 + m * Rnd)
If i1 < i2 And j1 < j2 Then
t = v(i1, j1)
v(i1, j1) = v(i2, j2)
v(i2, j2) = t
End If
Loop

Selection.Areas(1).Value2 = v
End Sub