VBA to randomly assign values to a cell

andrewb90

Well-known Member
Joined
Dec 16, 2009
Messages
1,077
Hello,

I am trying to make a macro that when clicked, will randomly choose one of 7 values and type that into cell C4. Values can be Dodgeball, Football, Soccer, Tetherball, Soccer, Baseball, Basketball. Is this doable?

Thanks!

Andrew
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
One way:

Code:
Sub x()
    ActiveCell.Value = [index({"Dodgeball", "Football", "Soccer", "Tetherball", "Soccer", "Baseball", "Basketball"}, randbetween(1,7))]
End Sub
 
Upvote 0
This works very well, however I was wondering if It could be changed to a specific cell instead of the active cell?
 
Upvote 0
Code:
Range("A1").Value = ...
 
Upvote 0
Quick question. I added a few more values and in in VBA I get a Compile Error: Identifier too long. (I changed the value at the end to 14 as well) Is there something that would cause this problem?
 
Upvote 0
Code:
Sub bottom()    Range("H5").Value = [index({"Basket 2 on two", "Foot 3 on 3", "Base 4 on 4", "Basket 2 on two", "Tether 1 on 1", "Soccer 3 on 3 or 1 on 1", "Golf 1 on 1", "Frisbee 4 on 4 or 5 on 5", "Curl anybody's game", "Shuffle 8x1", "Volley 2 on 2 or 3 on 3", "Crick 1 on 1", "Bad 1 on 1", "Tennis 2 on 2"}, randbetween(1,14))]
End Sub
 
Upvote 0
Code:
Sub bottom()
    Dim asSport()   As String

    asSport = Split("Basket 2 on two|Foot 3 on 3|Base 4 on 4|Basket 2 on two|" & _
                    "Tether 1 on 1|Soccer 3 on 3 or 1 on 1|Golf 1 on 1|" & _
                    "Frisbee 4 on 4 or 5 on 5|Curl anybody's game|Shuffle 8x1|" & _
                    "Volley 2 on 2 or 3 on 3|Crick 1 on 1|Bad 1 on 1|Tennis 2 on 2", "|")
    Range("H5").Value = asSport(Int(Rnd() * UBound(asSport)) + 1)
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,797
Messages
6,121,629
Members
449,041
Latest member
Postman24

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top