ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Worksheet Functions (https://www.excelbanter.com/excel-worksheet-functions/)
-   -   How do I set up a "Random Number" button in Excel? (https://www.excelbanter.com/excel-worksheet-functions/115911-how-do-i-set-up-random-number-button-excel.html)

jnoriega

How do I set up a "Random Number" button in Excel?
 
Im creating a quote price spreadsheet to keep track of what we quote our
customers and in one of the rows I would like to add a button that generates
a random number. This random number becomes the ID number so we can give to
our customer in case that they reach another representative. Is this
possible?

ExcelBanter AI

Answer: How do I set up a "Random Number" button in Excel?
 
Yes, it is definitely possible to create a button that generates a random number in Excel. Here are the steps to set it up:
  1. First, you need to enable the Developer tab in Excel. To do this, go to File Options Customize Ribbon. In the right-hand column, check the box next to "Developer" and click OK.
  2. Once the Developer tab is enabled, click on it and select "Insert" from the Controls group.
  3. In the "Form Controls" section, select the "Button" option.
  4. Click and drag on the worksheet to create the button.
  5. Right-click on the button and select "Assign Macro".
  6. In the "Assign Macro" dialog box, click on the "New" button to create a new macro.
  7. In the Visual Basic Editor window that opens, enter the following code:

    Formula:

    Sub GenerateRandomNumber()
        
    Range("A1").Value Int((999999 100000 1) * Rnd 100000)
    End Sub 

    This code generates a random number between 100000 and 999999 and puts it in cell A1. You can change the cell reference to whatever cell you want to use for the ID number.
  8. Close the Visual Basic Editor window and click "OK" in the "Assign Macro" dialog box.
  9. Test the button by clicking on it. It should generate a random number in the designated cell.

That's it! You now have a button that generates a random number in Excel. You can customize the code to generate a different range of numbers or to put the number in a different cell if needed.

Richard Buttrey

How do I set up a "Random Number" button in Excel?
 
On Tue, 24 Oct 2006 14:25:02 -0700, jnoriega
wrote:

I’m creating a quote price spreadsheet to keep track of what we quote our
customers and in one of the rows I would like to add a button that generates
a random number. This random number becomes the ID number so we can give to
our customer in case that they reach another representative. Is this
possible?



Just add a button from the toolbox, right click and put something like
the following code in the button's click event.


Private Sub CommandButton1_Click()
Randomize
ActiveCell = Round(Rnd * 1000, 0)
End Sub


But are you sure you want a random number? It's possible that you
might still duplicate a number. Why not control a normal variable
number by incrementing it by say 1, each time the button is pressed?

Rgds

__
Richard Buttrey
Grappenhall, Cheshire, UK
__________________________


All times are GMT +1. The time now is 05:20 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com