Button to Generate Random Cell from List

aeseya

New Member
Joined
Oct 12, 2023
Messages
5
Office Version
  1. 365
Platform
  1. Windows
Hello,

I'm trying to place a button which will randomly choose a cell from a table column and populate a target cell with its contents (or some similar way to 'show' the result). I don't have a lot of experience with excel and I've never used buttons before so I'm a bit out of my depth.

If relevant, I am using excel 365 on windows, but once it's set up I would be using the button on the excel ios app. The table is populated with text rather than numbers.

Any help would be much appreciated!
 
You can use this improved code. Target cell is selected as D5. D5 can be changed in the code to any required desired cell.
VBA Code:
Sub SelectRandomCell()
Dim tgt As Range
Dim X&, Lr&
Dim M
Set tgt = Range("D5")  'setting the target cell as D5
Lr = Range("C" & Rows.Count).End(xlUp).Row
M = Filter(Evaluate("transpose(if(Sheet1!C1:C" & Lr & "="""",False,row(Sheet1!C1:C" & Lr & ")))"), False, False)
X = WorksheetFunction.RandBetween(0, UBound(M))
tgt = Range("C" & M(X))
End Sub
This is fantastic! I was able to use this to create three different buttons on Sheet 1 to pull random data from either sheet 2, 3, or 4 by just changing the D5 range slightly for each new button as well as where it is pulling data from like this.
Quick question though, is there a simple way to add something to have it fully go through all the answers before repeating itself? I seem to get repeating answers often. I eventually do get all the answers, but sometimes it takes alot of clicks. (Im trying to make a list for my wife, and would like it to go through the whole thing before repeating if possible. )

This is basically how i changed it for my second button to pull data from sheet 3

Sub SelectRandomCell2()
Dim tgt As Range
Dim X&, Lr&
Dim M
Set tgt = Range("E5") 'setting the target cell as D5
Lr = Range("Sheet3!C" & Rows.Count).End(xlUp).Row
M = Filter(Evaluate("transpose(if(Sheet3!C1:C" & Lr & "="""",False,row(Sheet3!C1:C" & Lr & ")))"), False, False)
X = WorksheetFunction.RandBetween(0, UBound(M))
tgt = Range("Sheet3!C" & M(X))
End Sub
 
Upvote 0

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Try
VBA Code:
Sub SelectRandomCell()
Dim tgt As Range
Dim T&, Lr&
Dim M
Set tgt = Range("D5")  'setting the target cell as D5
Lr = Range("C" & Rows.Count).End(xlUp).Row
M = Filter(Evaluate("transpose(if(Sheet1!C1:C" & Lr & "="""",False,row(Sheet1!C1:C" & Lr & ")))"), False, False)

For T = 0 To UBound(M)
tgt.Offset(T, 0) = Range("C" & M(T))
Next T
End Sub
 
Upvote 0
Try
VBA Code:
Sub SelectRandomCell()
Dim tgt As Range
Dim T&, Lr&
Dim M
Set tgt = Range("D5")  'setting the target cell as D5
Lr = Range("C" & Rows.Count).End(xlUp).Row
M = Filter(Evaluate("transpose(if(Sheet1!C1:C" & Lr & "="""",False,row(Sheet1!C1:C" & Lr & ")))"), False, False)

For T = 0 To UBound(M)
tgt.Offset(T, 0) = Range("C" & M(T))
Next T
End Sub
That seemed to just display everything in the list from the other page all at once rather than go through them all one by one. I could easily be wrong though. It just didn't seem to work for me. I don't want to take up too much of your time though. I really appreciate the other code. It helped out a lot!
 
Upvote 0

Forum statistics

Threads
1,224,829
Messages
6,181,218
Members
453,024
Latest member
Wingit77

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