Solved

Excel Function To Randomly Select A Value In Excel Sheet

Posted on 2014-01-04
7
431 Views
Last Modified: 2014-01-09
Hello,

I am trying to create a function in excel to randomly select a value which will then go into a combo box when I click on a button and will then take that value so I can add it to another spread sheet.

On my second spreadsheet I have a list of values which I am trying use.
I know that in excel you can use the random function like this ?

=INDEX(Sheet2!$C:$C,RANDBETWEEN(1,COUNTA($C:$C)),1) . but how would I include this function into a combo box as this appears blank if I type it into the properties box of the combo box

Thanks.
0
Comment
Question by:Nightlife2004
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 81

Expert Comment

by:byundt
ID: 39756472
Here is a macro that will choose a random value in a Forms toolbar ComboBox, then put that value somewhere. It uses a named formula RandomValue that is similar to (but different from) the worksheet formula in your question.
Sub RandomValueMacro()
Dim v As Variant
Dim shp As Shape
v = Range("RandomValue").Value
Set shp = Worksheets("Sheet1").Shapes("Drop Down 2")
shp.ControlFormat.Value = v
Range("F5").Value = v
End Sub

Open in new window

RandomComboBoxSelectionQ28330547.xlsm
0
 

Author Comment

by:Nightlife2004
ID: 39756792
Hello,

I have tested the formula , it works great however I get some errors when running the macro if I changed the values to text based like string text.

Mismatch error code 13.

As I believe when I debug the code it only shows numbers for the controlformat.value where as the v variant is like a text string.

And one other note I am using activex control combo boxes in my excel spread sheet , how would I go about implementing that?
0
 
LVL 81

Expert Comment

by:byundt
ID: 39756825
I posted a sample file so you could see that the techniques I was suggested actually worked as desired. Since your actual workbook is different from mine and you are unable to adjust to the differences, please post a sample workbook so I can customize my code to match your data layout.
0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

Author Comment

by:Nightlife2004
ID: 39756841
Hello,

Ok I will send you the sample workbook which you sent me , I have slightly modified it.

Like all the number values I have changed to text strings like apple,banana etc.

I have include also an activex control combobox , stating that when I click on the drop down button I would like a random value selected from the list.

An error screenshot of the error which I get now when I run the macro.

That will be all thanks.
ComboBoxQ28330547.xlsm
0
 
LVL 81

Expert Comment

by:byundt
ID: 39756863
I changed the code so it refers to your ActiveX ComboBox control. I also allowed the user to select a specific value from the dropdown, as well as select a random value when you click on the ComboBox.
Sub RandomValueMacro()
Dim v As Variant
Dim shp As Shape
v = Range("RandomValue").Value
Worksheets("Sheet1").ComboBox1.Value = v
Range("F5").Value = v
End Sub

Open in new window


The code for ActiveX ComboBox events must go in the code pane for the worksheet that contains them. In your case, that would be the Sheet1 code pane. The GotFocus event occurs when the user selects the ComboBox. When that happens, the following event sub runs:
Private Sub ComboBox1_GotFocus()
RandomValueMacro
End Sub

Open in new window

ComboBoxQ28330547.xlsm
1
 
LVL 81

Accepted Solution

by:
byundt earned 500 total points
ID: 39756865
FWIW, I find ActiveX controls to be somewhat buggy on occasion. For this reason, I eschew them in preference to the older and more reliable Forms controls.
0
 

Author Closing Comment

by:Nightlife2004
ID: 39769093
Thanks Very Much For Your Help . Everything seems to be working now.
0

Featured Post

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
My attempt to use PowerShell and other great resources found online to simplify the deployment of Office 365 ProPlus client components to any workstation that needs it, regardless of existing Office components that may be needing attention.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question