Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 47
Default Random Sort selected cells

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?

Thanks,
Guy
  #2   Report Post  
Posted to microsoft.public.excel.worksheet.functions
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
  #3   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 47
Default Random Sort selected cells

That doesn't seem to do anything. I am just a little better than a novice at
VBA code, if the code needs to be modified for my application, I can't figure
out how.
I have a range of cells in column E that is selected, not always the same
number of cells are selected, and I need to randomly sort the selected cells
when I click on a forms button put on the sheet.

Thanks,
Guy

"Harlan Grove" wrote:

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

  #4   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 1,231
Default Random Sort selected cells

Guy wrote...
That doesn't seem to do anything. I am just a little better than a novice at
VBA code, if the code needs to be modified for my application, I can't figure
out how.

....

Begging the question why you're trying to do this.

You'd need to put the code in my previous response into a GENERAL VBA
module. For example, press [Alt]+[F11] to open the Visual Basic Editor
(VBE), then run the VBE menu command Insert Module. That should open
a window showing a new, empty general module. Paste my code into it.

Then press [Alt]+[F11] again to switch back to Excel. Right-click on
the form control you want to use to shuffle the selected cells'
values, and assign the macro foo to it.
  #5   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 47
Default Random Sort selected cells

I did that, it doesn't seem to be doing anything.
The column has names of people playing in a tournament. I need to randomly
sort the cells for bracket assignment. The column is E and the range
differs between rows 3 and 130 depending on number of participants. I have a
macro that selects all players in the column, I now need to randomly sort the
cells that are selected.

Thanks,
Guy

"Harlan Grove" wrote:

Guy wrote...
That doesn't seem to do anything. I am just a little better than a novice at
VBA code, if the code needs to be modified for my application, I can't figure
out how.

....

Begging the question why you're trying to do this.

You'd need to put the code in my previous response into a GENERAL VBA
module. For example, press [Alt]+[F11] to open the Visual Basic Editor
(VBE), then run the VBE menu command Insert Module. That should open
a window showing a new, empty general module. Paste my code into it.

Then press [Alt]+[F11] again to switch back to Excel. Right-click on
the form control you want to use to shuffle the selected cells'
values, and assign the macro foo to it.

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Random sort function for text Devin Excel Worksheet Functions 1 October 18th 07 10:44 PM
How can I sort a selection of cells into random order? IanS Excel Discussion (Misc queries) 2 June 14th 07 12:31 PM
Random Sort koba Excel Discussion (Misc queries) 3 March 16th 06 12:54 AM
I want to sort selected cells in Excel, not entire rows. Aeryn635 Excel Discussion (Misc queries) 1 June 1st 05 07:58 PM
How to sort random numbers in columns webehere Excel Discussion (Misc queries) 3 January 15th 05 12:24 PM


All times are GMT +1. The time now is 11:31 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"