Need help coding a "generate random" button

StarckR

New Member
Joined
Jan 28, 2021
Messages
3
Office Version
  1. 365
Platform
  1. Windows
I'm creating a button to help my coworkers be able to automatically generate a book from a list of our books.
I want to use the following code in a button =INDEX($A:$A,RANDBETWEEN(1,COUNTA($A:$A)),1) so that all she has to do is click the button and it generates a random book title for her.
But I'm unsure how to code it correctly. Could someone please help me with this?

Thank you!!
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Hi & welcome to MrExcel.
If the button is on the same sheet as the data try
VBA Code:
Sub StarckR()
   Range("I3") = Evaluate("INDEX($A:$A,RANDBETWEEN(1,COUNTA($A:$A)),1)")
End Sub
Change I3 to the cell you want the output.
 
Upvote 0
Hi & welcome to MrExcel.
If the button is on the same sheet as the data try
VBA Code:
Sub StarckR()
   Range("I3") = Evaluate("INDEX($A:$A,RANDBETWEEN(1,COUNTA($A:$A)),1)")
End Sub
Change I3 to the cell you want the output.
There is already some code in the button, so will it look like this:

Private Sub CommandButton1_Click()
Sub StarckR()
Range("I3") = Evaluate("INDEX($A:$A,RANDBETWEEN(1,COUNTA($A:$A)),1)")
End Sub
End Sub

I tried that but got an error :(
 
Upvote 0
In that case just add this to your existsing button code
VBA Code:
Range("I3") = Evaluate("INDEX($A:$A,RANDBETWEEN(1,COUNTA($A:$A)),1)")
 
Upvote 0
Solution
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,750
Members
448,989
Latest member
mariah3

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