Using ActiveX CommandButton to create random password

thekamel

New Member
Joined
Feb 11, 2015
Messages
16
Office Version
  1. 365
  2. 2021
  3. 2019
Platform
  1. Windows
  2. MacOS
  3. Mobile
  4. Web
Hello everyone. I am trying to create an ActiveX CommandButton that will create a randomly generated password. I have searched and cannot find anything about using ActiveX to create passwords, I can only find how to create the option in Excel only. Is anyone able to assist me with this?
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
A button is just a pointer to run a piece of code, you have to put the code somewhere for the button to point to. Here's some code to put into an Excel sheet that, when you run it will create a random length (6 to 12 characters hard coded) string of random characters and show it to you in a message box for you to copy down. You can then assign that to a button. Is that what you're looking for?

VBA Code:
Sub reallyrando()
'https://www.mrexcel.com/board/threads/using-activex-commandbutton-to-create-random-password.1208398/
'by portews

Dim randtime as integer, randchar as integer
Dim i as integer, newchar, x
dim newPW as string
'get a random amount of characters between 6 and 12 long
randtime = GetRando(6, 12)

'for each of those characters...
For i = 1 To randtime
    'get a random character based on the ASCII table for easily typed characters
    newchr = GetRando(33, 126)
    'if something went wrong, quit
    If newchr = "Invalid" Then x = MsgBox("Invalid Parameters", vbOKOnly, "New Password"): End
    'concatenate the string with the new character
    newPW = newPW & Chr(newchr)
    'erase your temporary character and do it again
    newchr = ""
Next
'print out your new password in an input box so you can copy it easily
x = MsgBox("New " & randtime & " character password" & vbCrLf & newPW, vbOKOnly, "New Password")
    
End Sub

Function GetRando(lower As Integer, upper As Integer)
Randomize CDbl(Now())
If lower < upper Then
    GetRando = Int((upper - lower + 1) * Rnd + lower)
Else
    GetRando = "Invalid"
End If
End Function
 
Upvote 0
Solution
A button is just a pointer to run a piece of code, you have to put the code somewhere for the button to point to. Here's some code to put into an Excel sheet that, when you run it will create a random length (6 to 12 characters hard coded) string of random characters and show it to you in a message box for you to copy down. You can then assign that to a button. Is that what you're looking for?

VBA Code:
Sub reallyrando()
'https://www.mrexcel.com/board/threads/using-activex-commandbutton-to-create-random-password.1208398/
'by portews

Dim randtime as integer, randchar as integer
Dim i as integer, newchar, x
dim newPW as string
'get a random amount of characters between 6 and 12 long
randtime = GetRando(6, 12)

'for each of those characters...
For i = 1 To randtime
    'get a random character based on the ASCII table for easily typed characters
    newchr = GetRando(33, 126)
    'if something went wrong, quit
    If newchr = "Invalid" Then x = MsgBox("Invalid Parameters", vbOKOnly, "New Password"): End
    'concatenate the string with the new character
    newPW = newPW & Chr(newchr)
    'erase your temporary character and do it again
    newchr = ""
Next
'print out your new password in an input box so you can copy it easily
x = MsgBox("New " & randtime & " character password" & vbCrLf & newPW, vbOKOnly, "New Password")
   
End Sub

Function GetRando(lower As Integer, upper As Integer)
Randomize CDbl(Now())
If lower < upper Then
    GetRando = Int((upper - lower + 1) * Rnd + lower)
Else
    GetRando = "Invalid"
End If
End Function
This is very close to what I am looking for! Is there a way to get it to put the password in a cell instead of a message box?
 
Upvote 0
Instead of the
x= MsgBox... line, put
VBA Code:
Range("A1") = newPW
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,875
Members
452,363
Latest member
merico17

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