MrExcel Message Board

Go Back   MrExcel Message Board > Question Forums > Excel Questions

Excel Questions All Excel/VBA questions - formulas, macros, pivot tables, general help, etc. Please post to this forum in English only.

Reply
 
Thread Tools Display Modes
Old Mar 8th, 2002, 02:04 AM   #1
mcconns
New Member
 
Join Date: Mar 2002
Posts: 1
Default

Aim is to create a random cup draw.

I enter 32 players names in cells A1 - A32

I want to generate random numbers between 1 and 32. At no time should any random number generated be repeated.

In addition when each random number is generated I want it to recognise a players name and copy his name into say cell C1.

The next random number generated should follow the above process but this players name should be copied in cell E1.

Next name to cell C2, followed by cell E2 and so on until we have 16 players names in cells C1-C16 and 16 players names in cells E1-E16
mcconns is offline   Reply With Quote
Old Mar 8th, 2002, 03:30 AM   #2
Escalus
 
Join Date: Feb 2002
Posts: 12
Default

Try this :-

Sub Randomize_List()
Application.ScreenUpdating = False
[A1:A32].Copy [C1]
Columns("C:D").Insert
[D1:D32].FormulaR1C1 = "=RAND()"
[D1:E32].Sort Key1:=[D1], Header:=xlNo
[D1:D32].Value = [D1:D32].Value
[C1:C32].FormulaR1C1 = "=IF(MOD(ROW(),2)<>0,1,2)"
[C1:C32].Value = [C1:C32].Value
[C1:E32].Sort Key1:=[C1], Header:=xlNo
[E17:E32].Cut
ActiveSheet.Paste Destination:=[G1]
Columns("C:D").Delete
End Sub





[ This Message was edited by: Escalus on 2002-03-08 02:35 ]
Escalus is offline   Reply With Quote
Old Mar 8th, 2002, 03:58 AM   #3
Dave Hawley
Banned
 
Join Date: Feb 2002
Posts: 1,582
Default

Hi mcconns

Here is some that will create random numbers for.


Sub RandomNumberGenerator()
'Creates a list of random numbers _
between 1 and 36 in range A1:F6
'Written by OzGrid Business Applications
'www.ozgrid.com
Dim Rw As Integer, Col As Integer
Dim Reply1 As Long, Reply2 As Long, Reply3 As Long
Dim lSqR As Long
Dim lSqC As Long
Dim i As Long, lRand As Long
On Error Resume Next




Above:
Reply2 = 0
Reply2 = InputBox("Lowest number ?" _
& Chr(13) & Chr(13) & "Number must be greater than 0 and entered without spaces or commas" & Chr(13) _
, "OzGrid Random Number Generator", 1)
If Reply2 = 0 Then Exit Sub

If Reply2 < 1 Then
MsgBox "Number must be greater than 0", vbCritical, "OzGrid Business Applications"
GoTo Above
End If

Above2:
Reply3 = 0
Reply3 = InputBox("Highest number ? " _
& Chr(13) & Chr(13) & "Number must be greater than " & Reply2 & " and entered without spaces or commas" & Chr(13) _
, "Lowest number = " & Reply2, 500)
If Reply3 = 0 Then Exit Sub

If Reply3 <= Reply2 Then
MsgBox "Number must be greater than " & Reply2, vbCritical, "OzGrid Business Applications"
GoTo Above2
End If


HowMany:
Reply1 = 0
Reply1 = InputBox("How many random numbers do you wish to generate?" _
& Chr(13) & Chr(13) & "Number must be whole and less than 1000" & Chr(13) _
& Chr(13) & "An amount greater than 500 within a tight band will take some time!" _
& Chr(13) & "Numbers will be sorted Left to Right by row" _
, "Lowest number = " & Reply2 & " Highest number = " & Reply3, 200)
If Reply1 = 0 Then Exit Sub

If Reply1 > 1000 Then
MsgBox "Number must be less than 1000", vbCritical, "OzGrid Business Applications"
GoTo HowMany:
End If

If Reply1 > Reply3 - Reply2 Then
MsgBox "Not possible.", vbCritical, "OzGrid Business Applications"
GoTo HowMany:
End If

Application.DisplayAlerts = False
Application.ScreenUpdating = False

Sheets.Add().Name = "Random Numbers"
If ActiveSheet.Name <> "Random Numbers" Then
ActiveSheet.Delete
Sheets("Random Numbers").Select
Cells.Clear
End If
Dim sCheck As String
Range("A1") = Reply1

Range("B1").FormulaR1C1 = "=ROUNDUP(SQRT(RC[-1]),0)"




lSqR = Range("B1")
lSqC = lSqR

On Error GoTo 0
'Clear the range ready for random numbers
Range("A1:IV600").Clear

Randomize ' Initialize random-number generator.
For Col = 1 To lSqC 'Set the Column numbers
If WorksheetFunction.CountA(Cells) = Reply1 Then Exit For
For Rw = 1 To lSqR 'Set the Row numbers
If WorksheetFunction.CountA(Cells) = Reply1 Then Exit For

'Cells(Rw, Col) = Int((Reply3 - Reply2 + 1) * Rnd + Reply2)

Do Until WorksheetFunction.CountIf _
(Cells, Cells(Rw, Col)) = 1
Cells(Rw, Col) = Int((Reply3 - Reply2 + 1) * Rnd + Reply2)
Loop
If WorksheetFunction.CountA(Cells) = Reply1 Then Exit For
Next Rw
If WorksheetFunction.CountA(Cells) = Reply1 Then Exit For
Next Col

Rw = 1
For Rw = 1 To ActiveSheet.UsedRange.Rows.Count
Rows(Rw).Sort Key1:=Rows(Rw).Range("A1"), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlLeftToRight
Next Rw
Application.Goto Range("A1"), True

Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
Dave Hawley is offline   Reply With Quote
Old Mar 8th, 2002, 05:46 AM   #4
Ian Mac
MrExcel MVP
 
Join Date: Feb 2002
Location: Newcastle, UK
Posts: 1,174
Default

Here's a formula solution,

in A1:A32 put:

=Rand() (place in A1 & copy down)

in B1:B32 put:

=RANK(A1,$A$1:$A$32)+COUNTIF(B$1:B1,B1)-1
Exactly as it's writtem with the $'s in the same places.

in C1 put:

your Players Names

Now in any column you want (I'm using E), put the numbers 1 - 16, then 2 columns along from that (G for me) put 17 - 32.

now all you need to do is use vlookup in cols F + H on the table you've just created range B1:C32.

the easiest way is to put:

=VLOOKUP(E1,$B$1:$C$32,2,0)

in F1 and copy down. then copy paste F1:F16 into H1:H16. (again make sure the $'s are in the right place).

you can now hide Cols B,C,E and G. Every time you press Del, F9 or recalc the sheet you'll get a random list of matches.

Hope this helps.
__________________
"Have a good time......all the time"
Ian Mac
Ian Mac is offline   Reply With Quote
Old Mar 8th, 2002, 06:16 AM   #5
Escalus
 
Join Date: Feb 2002
Posts: 12
Default

Quote:
On 2002-03-08 04:46, Ian Mac wrote:
Here's a formula solution,

in A1:A32 put:

=Rand() (place in A1 & copy down)

in B1:B32 put:

=RANK(A1,$A$1:$A$32)+COUNTIF(B$1:B1,B1)-1
Exactly as it's writtem with the $'s in the same places.

in C1 put:

your Players Names

Now in any column you want (I'm using E), put the numbers 1 - 16, then 2 columns along from that (G for me) put 17 - 32.

now all you need to do is use vlookup in cols F + H on the table you've just created range B1:C32.

the easiest way is to put:

=VLOOKUP(E1,$B$1:$C$32,2,0)

in F1 and copy down. then copy paste F1:F16 into H1:H16. (again make sure the $'s are in the right place).

you can now hide Cols B,C,E and G. Every time you press Del, F9 or recalc the sheet you'll get a random list of matches.

Hope this helps.
The macro I posted is also based on recorded code from the following :-

- Players names in A1:A32
- Copy players names to C1:C32
- Insert 2 columns before column C
- In D1:D32 enter =RAND()
- Convert D1:D32 to values
- Select D1:E32 and sort
- In C1:C32 enter =IF(MOD(ROW(),2)<>0,1,2)
- Convert C1:C32 to values
- Select C1:C32 and sort
- Cut E17:E32 and paste to G1
- Delete columns C:D
Escalus is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT -4. The time now is 06:17 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
All contents Copyright 1998-2012 by MrExcel Consulting.
diabetic desserts recipes recipes Diabetic Soups Holiday Pizza Recipes Popcorn Recipes Recipes For Microwave Pasta Recipes Casserole Recipes Chili Recipes Curry Recipes Crockpot Recipes Apples Recipes Bread Recipes Vegetarian Recipes Vegetable recipes Desserts Recipes Appetizers Ethnic Recipes Meat Dishes Barbecue Recipes Sauces Recipes Marinade Recipes Low Fat Recipes Frugal Gourmet Kitchen Classics Recipes On The Grill Cook Books Seafood Recipes Cajun Recipes Breads Low Fat Low Fat Breads Bread Machine Recipes Yeast Breads Quick Breads Fat Free Vegetarian Salad Recipes Eggplant Recipes Radish Recipes Tomato Recipes Jalapeno Recipes Potato Recipes Lettuce Recipes Cabbage Recipes Beans Ambrosia Recipes Biscotti Recipes Desserts Low Fat Cookie Recipes Cheesecake Recipes Cake Recipes Pie Recipes Muffin Recipes Custard Recipes Best Appetizers Appetizers Low Fat Salsa Recipes Dip Recipes International Recipes Afghan Recipes Alaska Recipes French Recipes German Recipes Greek Recipes Italian Recipes Spanish Recipes Thai Recipes Korean Recipes Chinese Recipes Mexican Recipes Indian Recipes Beef Recipes Pork Pork & Ham Pork Butts Pork Chop Recipes Pork Ribs Rulled Pork Poultry Recipes Stews Recipes Ground Beef Barbecue Grill Barbecue Smoker All Purpose Sauce BBQ Sauce Barbecue Sauce Carolina BBQ Sauce Pickle Recipes Marinades Smoking Low Fat Appetizers & Dips Low Fat Breakfast Low Fat Cakes Low Fat Cheesecakes Low Fat Cookies Low Fat Desserts Low Fat Fish & Seafood Low Fat Meats Low Fat Pasta Low Fat Pies Low Fat Salads Low Fat Sandwiches Low Fat Sauces & Condiments Low Fat Sides Low Fat Soups Low Fat Vegetarian Baker's Dozen Taste of Home Recipe Book Bon Appetit Cookbook Blacktie Cookbook Buster Cook Book Cookbook USA Cook Book Cook Book Sara's Cookbook Sara's Cookbook Appetizers and Dips Poultry recipes Diabetic recipes Holiday recipes Miscellaneous recipes 110 recipes 1986 Usenet cookbook 2900 recipes Cyberrealm recipes Great sysops of world Specialty recipes Ceideburg recipes Cheese recipes Chili recipes Fruits recipes Garlic recipes Great chefs of NY Londontowne recipes Raisins recipes Recipes for kids US Food Vegetarian recipes Bread recipes Drinks Meat Dishes Brisket recipes Caribou recipes Chicken recipes Filet mignons recipes Pork recipes Swordfish recipes Turkey recipes Pasta recipes Uncategorized recipes Ethnic recipes Canada recipes English recipes Ethiopia recipes Germany recipes Greece recipes Mexican recipes Philippines recipes Welsh recipes Microwave recipes Soups recipes Vegetable recipes Asparagus recipes Barley recipes Brown rice recipes Lentil recipes Mushrooms recipes Salads recipes Wild rice Desserts recipes Cakes recipes Chocolate recipes Cookies recipes Ice cream recipes