Generate Random Number Button

csimpson24

New Member
Joined
Feb 22, 2018
Messages
22
Hi all,

I am wanting to have a random number generate button that generates a random number (between 5000 and 20000) in cell J2. I am needing to distribute the file as a XLTX. I would like for the button to only show until it is clicked to generate the random number. The workbook is protected and Cell J2 is locked so it would need to unlock, write the number then lock again and then disappear. Is this all possible?

Thank you,
Chris
 
Last edited:

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Hi.

.XLTX files are macro-disabled. You can't execute any vba code in such a file.
In order to make a macro work you'll have to save it as a .XLTM file

To generate a random number between 5000 and 20000 without VBA you can use this function in J2.

=INT(RAND()*15000+5000)
 
Last edited:
Upvote 0
Thank you. I totally forgot about that. I will distribute a .XLTM then.

My concern is that I only want to generate a number only the one time and have it never change in the .XLSX file.
 
Upvote 0
Get a Button from the Forms menu and attach this macro to it.

Code:
Sub Button1_Click()
    With ActiveSheet
        .Unprotect
        .Range("J2") = WorksheetFunction.RandBetween(5000, 20000)
        .Shapes(Application.Caller).Visible = False
        .Protect
    End With
End Sub
 
Upvote 0
Thank you! Is it possible to prevent them from clicking, seeing, or executing the macro if they have the XLTM file opened? Or can I prevent them from opening the actual XLTM file without having a password?
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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