Password Generator in excel FILE

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Hello. I built a function you can use to generate random passwords without repeating any characters. Can call it using VBA or from a worksheet function.

Code:
Public Function GeneratePassword(Optional ByVal length As Integer = 16) As String

' ============= CHARACTER CODES ============
' Punctuation : 33-47, 58-64, 91-96, 123-126
' Digits      : 48-57
' Uppercase   : 65-90
' Lowercase   : 97-122
' ==========================================
  
  Dim chars As New Collection
  Dim nextId As Integer
  Dim counter As Integer
  Dim i As Integer
  
  On Error GoTo ErrorHandler
  If length < 1 Or length > 94 Then
    Err.Raise vbObjectError + 513, "GeneratePassword", "Argument out of range."
  End If
  
  For i = 33 To 126
    chars.Add Chr(i)
  Next i
  
  Do While counter < length
    nextId = Int(chars.Count * Rnd()) + 1
    GeneratePassword = GeneratePassword & chars(nextId)
    chars.Remove nextId
    counter = counter + 1
  Loop
  
ExitHandler:
  On Error Resume Next
  Do While chars.Count > 0
    chars.Remove 1
  Loop
  Set chars = Nothing
  Exit Function
  
ErrorHandler:
  GeneratePassword = vbNullString
  Resume ExitHandler
End Function
 
Upvote 0
Hello. I built a function you can use to generate random passwords without repeating any characters. Can call it using VBA or from a worksheet function.

Code:
Public Function GeneratePassword(Optional ByVal length As Integer = 16) As String

' ============= CHARACTER CODES ============
' Punctuation : 33-47, 58-64, 91-96, 123-126
' Digits      : 48-57
' Uppercase   : 65-90
' Lowercase   : 97-122
' ==========================================
  
  Dim chars As New Collection
  Dim nextId As Integer
  Dim counter As Integer
  Dim i As Integer
  
  On Error GoTo ErrorHandler
  If length < 1 Or length > 94 Then
    Err.Raise vbObjectError + 513, "GeneratePassword", "Argument out of range."
  End If
  
  For i = 33 To 126
    chars.Add Chr(i)
  Next i
  
  Do While counter < length
    nextId = Int(chars.Count * Rnd()) + 1
    GeneratePassword = GeneratePassword & chars(nextId)
    chars.Remove nextId
    counter = counter + 1
  Loop
  
ExitHandler:
  On Error Resume Next
  Do While chars.Count > 0
    chars.Remove 1
  Loop
  Set chars = Nothing
  Exit Function
  
ErrorHandler:
  GeneratePassword = vbNullString
  Resume ExitHandler
End Function
Thank you for your code, but the problem is i have some special character in my password like math symbol, japan character,... So it is impossible to use the code 33-47, 58-64, 91-96, 123-126, moreover, I have create this excel file and use it in Android smartphone, so is there any way to solve my fuction to prevent duplicate character
Thank you for your support
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,249
Members
452,623
Latest member
Techenthusiast

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