ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Macro to randomize a range (https://www.excelbanter.com/excel-programming/282922-macro-randomize-range.html)

david wright

Macro to randomize a range
 
I have a set of numbers in cells (a1:a20) that I would
like to have resorted in random order each time I open the
workbook. I have searched the newsgroups for excel and I
cannot find one.

Any help would be appreciated
Thanks David

Tom Ogilvy

Macro to randomize a range
 
Private Sub Workbook_Open()
With ThisWorkbook.Worksheets("Sheet1")
.Columns(2).Insert
.Range("B1:B20").Formula = "=rand()"
.Range("A1:B20").Sort Key1:=.Range("B1"), _
Order1:=xlAscending
.Columns(2).Delete
End With
End Sub

go to the vbe and in the project explorer (treeview) select your
project/workbook. Double click on the thisWorkbook entry in the treeview
for your project. Paste in the above code. If there is an additional
declaration

Private sub Workbook_Open()

End sub

then delete that.

Save the workbook.
--
Regards,
Tom Ogilvy



david wright wrote in message
...
I have a set of numbers in cells (a1:a20) that I would
like to have resorted in random order each time I open the
workbook. I have searched the newsgroups for excel and I
cannot find one.

Any help would be appreciated
Thanks David




david wright

Macro to randomize a range
 
Tom,
Thank You Very Much

DW

-----Original Message-----
Private Sub Workbook_Open()
With ThisWorkbook.Worksheets("Sheet1")
.Columns(2).Insert
.Range("B1:B20").Formula = "=rand()"
.Range("A1:B20").Sort Key1:=.Range("B1"), _
Order1:=xlAscending
.Columns(2).Delete
End With
End Sub

go to the vbe and in the project explorer (treeview)

select your
project/workbook. Double click on the thisWorkbook entry

in the treeview
for your project. Paste in the above code. If there is

an additional
declaration

Private sub Workbook_Open()

End sub

then delete that.

Save the workbook.
--
Regards,
Tom Ogilvy



david wright wrote

in message
...
I have a set of numbers in cells (a1:a20) that I would
like to have resorted in random order each time I open

the
workbook. I have searched the newsgroups for excel and I
cannot find one.

Any help would be appreciated
Thanks David



.



All times are GMT +1. The time now is 04:55 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com