Selecting Random Cells Using VBA

omagoodness

Board Regular
Joined
Apr 17, 2016
Messages
67
I am creating a game board where I want to generate a random response to a listBox selection. My list box displays a list of categories. The player selects a category and I want Excel to select a random word/name etc. from a correcsponding list. For example, one list item is "The Oscars". I have a list with the names of 10 Oscar winners and I want excel to randomly select from the list of 10 names. In the sheet, I can accomplish this with the formula
Excel Formula:
=INDEX(Lists!H17:H27,RANDBETWEEN(1,10))
.

I have a user form for player interaction where the player selects from a list of 10 categories. Each category has a correcpsonding table of data and I have created variables for each list. I want to use VBA to make the random selection, but I cannot figure out how to translate the above formula to vba code. Can anyone help?
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
VBA Code:
Sub getRnd()
Dim varRnd As Variant

Randomize
varRnd = Int(9 * Rnd + 1)
Debug.Print varRnd

End Sub
10 values is a small sample size to work with, so you could find that the same value is picked if you run that in quick succession. You may need a module level variable of some type, to run the sub again if the last number was the same. If you want to ensure that the same number in a category is not repeated, that variable probably has to be an array. The the variable would need to be reset if you change the category. Anyway, I hope I answered the question, but as usual, it comes with what-if's.
 
Upvote 0
In answer to your specific question (how to express your formula in VBA code), try:

VBA Code:
Dim s As String
s = WorksheetFunction.Index(Worksheets("Lists").Range("H17:H27"), WorksheetFunction.RandBetween(1, 10))

then use the variable s as you need to.
 
Upvote 0
Solution
In answer to your specific question (how to express your formula in VBA code), try:

VBA Code:
Dim s As String
s = WorksheetFunction.Index(Worksheets("Lists").Range("H17:H27"), WorksheetFunction.RandBetween(1, 10))

then use the variable s as you need to.
Perfect. Thank you.
 
Upvote 0
VBA Code:
Sub getRnd()
Dim varRnd As Variant

Randomize
varRnd = Int(9 * Rnd + 1)
Debug.Print varRnd

End Sub
10 values is a small sample size to work with, so you could find that the same value is picked if you run that in quick succession. You may need a module level variable of some type, to run the sub again if the last number was the same. If you want to ensure that the same number in a category is not repeated, that variable probably has to be an array. The the variable would need to be reset if you change the category. Anyway, I hope I answered the question, but as usual, it comes with what-if's.
Thank you for your reply. I opted to use the method provided by kevin999 as I understand it better and I learn and remember when I have a clear understanding of what I am doing rather than just copying from someone else.
 
Upvote 0

Forum statistics

Threads
1,225,477
Messages
6,185,218
Members
453,283
Latest member
Shortm88

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