Random Password Generator

farhad

New Member
Joined
May 18, 2009
Messages
41
Good Day

I have the following VB code that generates a 10 character alphanumeric password. This code works perfectly fine.

However I would like to change the code to include special characters in the password.

The characters are limited to !@#$&*

Code:
Sub TenCharPassword()
    Dim i As Integer, c As Integer, n As Integer, pw As String
    For i = 1 To 100
        pw = ""
        For n = 1 To 10
            c = Evaluate("=RandBetween(65,100)")
            If c > 90 Then c = c
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
The function below will generate a sequence of N characters including 0-9, A-Z, a-z, and !@#$&*. The subroutine uses N=10.
Code:
Sub TenCharPassword()
MsgBox RandNumLet(10)
End Sub

Function RandNumLet(numChars As Integer)
'Generates a string, numChars in length, of random alphanumerics (0-9, A-Z, a-z)and special characters (!@#$&*)
'Can be used on a worksheet --> =RandNumLet(x)
If Not IsNumeric(numChars) Or Not numChars > 0 Then
    RandNumLet = CVErr(xlErrNA)
    Exit Function
End If
Dim picknum As Long
Dim Lets As String
Dim Nums As String
Dim Specials As String
Dim FinalResult As String
Application.Volatile
Randomize
Do
    picknum = Int((4 * Rnd) + 1)
    Select Case picknum
        Case 1: Nums = CStr(Int(10 * Rnd)) 'generates i-digit string from 0-9
        Case 2: Lets = Chr(Int((26 * Rnd) + 65)) 'generates A-Z
        Case 3: Lets = Chr(Int((26 * Rnd) + 97))  'generates a-z
        Case 4: Specials = WorksheetFunction.Choose(Int((5 * Rnd) + 1), Chr(33), Chr(35), Chr(36), Chr(38), Chr(42), Chr(64))
    End Select
    FinalResult = FinalResult & Nums & Lets & Specials
    Nums = ""
    Lets = ""
    Specials = ""
Loop Until Len(FinalResult) >= numChars
RandNumLet = FinalResult
End Function
 
Upvote 0
Code:
Function TenCharPassword() As String
  Const sSym As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$&*"
  Dim i As Long
  
  For i = 1 To 10
    TenCharPassword = TenCharPassword & Mid(sSym, [=RandBetween(1,32)], 1)
  Next i
End Function

[Table="width:, class:grid"][tr][td="bgcolor:#C0C0C0"][/td][td="bgcolor:#C0C0C0"]
A​
[/td][td="bgcolor:#C0C0C0"]
B​
[/td][/tr][tr][td="bgcolor:#C0C0C0"]
1​
[/td][td]W&&&VMPCWG[/td][td]A1: =TenCharPassword()[/td][/tr]
[tr][td="bgcolor:#C0C0C0"]
2​
[/td][td]LEKAGRXRIT[/td][td][/td][/tr]
[tr][td="bgcolor:#C0C0C0"]
3​
[/td][td]BUNIWWF!NW[/td][td][/td][/tr]
[tr][td="bgcolor:#C0C0C0"]
4​
[/td][td]NO*CS&PBTB[/td][td][/td][/tr]
[tr][td="bgcolor:#C0C0C0"]
5​
[/td][td]FWWYKQBTPK[/td][td][/td][/tr]
[/table]
 
Upvote 0
Dear JoeMo

Thanks for the Code works perfectly fine. I would like to generate a 100 passwords and copy in column A

Code:
Sub TenCharPassword()
MsgBox RandNumLet(10)
End Sub

Thanks
 
Upvote 0
Code:
Sub farhad()
  With Range("A1:A100")
    .Formula = "=TenCharPassword()"
    .Value = .Value
  End With
End Sub
 
Upvote 0
Code:
Sub TenCharPassword()
Dim i as long
Application.ScreenUpdating = False
For i = 1 to 100
    Cells(i,"A").Value = RandNumLet(10)
Next i
Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,819
Messages
6,181,153
Members
453,021
Latest member
Justyna P

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