Password Generator w/specific requirements

MyriadRocker

New Member
Joined
Mar 22, 2010
Messages
12
Hey everyone! I have a need to do a password generator. I have found some great code here but I can't find anything that gets my specific needs met. Here's what I'm looking for...

I need an Excel sheet (Sheet1) that contains a storage for passwords. Let's say the first password in A1 is "Kr$ft##3". That's the first, and only, password there for now.

I want to be able to generate another password in the cell below it whenever I click a button. That would put a password in A2. Then if I click it again, it puts a password in A3. So on and so forth.

However, these are the password requirements:


  • a maximum of 2 repeated characters in the password
  • a minimum of 3 characters not found in previous password
  • a minimum of 3 alphabetic characters
  • a minimum of 2 non-alphabetic characters
  • a minimum of 8 characters in length but not more than 14 in length
  • new password cannot be the same as previous 20 passwords
These requirements are a bit daunting! I'm not sure where to start!
 
Yea. Adjusting array size dynamically in VBA is impossible I think...
I think in C, you might be able to use a pointer to adjust it dynamically but
in VBA, you have to declare a variable as a constant if you want to use it as an array size.

So, the code you've posted, is it not working cuz of the problem mentioned above?

If not, where is it highlighting when you press Debug?
 
Upvote 0

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Slight change...

Code:
Sub PasswordGenerator()
    Dim PasswordArray(14) As String
    Dim Password As String
    Dim PasswordLength As Integer
    Dim CharCnt As Integer
    Dim RandomChar As String
    Dim LAC As Integer 'Lowest ASCII Character
    Dim HAC As Integer 'Highest ASCII Character
    Dim UseNonAlphas As Boolean
    Dim HasNonAlphas As Boolean
    Dim RandomNumber As Integer
    Dim NumOfPasswords As Integer
    Dim NumPasswordsToCompare As Integer
    Dim i As Long, x As Long
    Dim RowNum As Integer
    Dim AlphaCnt As Integer
    Dim NonAlphaCnt As Integer
    
    'Counts for alphabetic & non-alphabetic characters
    AlphaCnt = 0
    NonAlphaCnt = 0
    
    'The number of consecutive passwords to generate
    NumOfPasswords = 1
    
    'The number of passwords to compare to make sure the password doesn't already exist
    NumPasswordsToCompare = 20
    
    'Produces a random password length between 8 and 14 characters
    PasswordLength = (Int((14 - 8 + 1) * Rnd + 8))
    
    'Visit www.lookuptables.com for the ASCII table.
    LAC = Asc("0")
    HAC = Asc("z")
    
    'Flag to use special characters or not
    UseNonAlphas = True
    
GeneratePassword:

    'Looking for a blank cell to put the password in
    RowNum = 1
    ActiveSheet.Range("A1").Select
    Do Until IsEmpty(ActiveCell)
        RowNum = RowNum + 1
        ActiveCell.Offset(1, 0).Select
    Loop
    
    For i = 1 To NumOfPasswords
        Erase PasswordArray
        Password = ""
        For CharCnt = 1 To PasswordLength
            Do
                RandomNumber = Int((HAC - LAC + 1) * Rnd + LAC)
                RandomChar = Chr(RandomNumber)
                'Checking for non-alphabetics & alphabetics
                If (RandomNumber >= 33 And RandomNumber <= 47) Or _
                    (RandomNumber >= 58 And RandomNumber <= 64) Or _
                    (RandomNumber >= 91 And RandomNumber <= 96) Or _
                    (RandomNumber >= 123 And RandomNumber <= 126) Then
                    HasNonAlphas = True
                Else
                    HasNonAlphas = False
                End If
                'Counting non-alphabetics & alphabetics
                If HasNonAlphas = True Then
                    NonAlphaCnt = NonAlphaCnt + 1
                Else
                    AlphaCnt = AlphaCnt + 1
                End If
            Loop Until UseNonAlphas = True Or HasNonAlphas = False
            PasswordArray(CharCnt) = RandomChar
            Password = Password & RandomChar
        Next CharCnt
        
        'Minimum of 3 alphabetic characters
        If AlphaCnt < 3 Then
            GoTo GeneratePassword
        End If
        
        'Minimum of 2 non-alphabetic characters
        If NonAlphaCnt < 2 Then
            GoTo GeneratePassword
        End If
        
        'Maximum of 2 repeated characters
        For x = 1 To PasswordLength - 2
            If PasswordArray(x) = PasswordArray(x + 1) Then
                If PasswordArray(x) = PasswordArray(x + 2) Then
                    GoTo GeneratePassword
                End If
            End If
        Next x
        
        'Minimum of 3 characters not found in previous password
        If RowNum > 1 Then 'Making sure a previous password exists
            Dim PrevPasswordArray(14) As String
            Dim PrevSwitch As Integer
            Dim EqualSwitch As Boolean
            
            For x = 1 To Len(ActiveSheet.Range("A" & RowNum - 1))
                PrevPasswordArray(x) = Mid(ActiveSheet.Range("A" & RowNum - 1), x, 1)
            Next x

            PrevSwitch = 0
            
            For cnt1 = 1 To PasswordLength
                EqualSwitch = False
                For cnt2 = 1 To Len(ActiveSheet.Range("A" & RowNum - 1))
                    If PasswordArray(cnt1) = PrevPasswordArray(cnt2) Then
                        EqualSwitch = True
                    End If
                Next cnt2
                If EqualSwitch = False Then
                    PrevSwitch = PrevSwitch + 1
                End If
            Next cnt1
            If PrevSwitch < 3 Then
                GoTo GeneratePassword
            End If
        End If
        
        'Cannot be the same as previous NumPasswordsToCompare passwords (also works for less than NumPasswordsToCompare)
        If RowNum > NumPasswordsToCompare Then
            For x = RowNum - 1 To RowNum - NumPasswordsToCompare Step -1
                If ActiveSheet.Range("A" & i).Value = Password Then
                    GoTo GeneratePassword
                End If
            Next x
        Else
            For x = 1 To RowNum - 1
                If ActiveSheet.Range("A" & i).Value = Password Then
                    GoTo GeneratePassword
                End If
            Next x
        End If
        
        'Finally placing the password
        ActiveSheet.Range("A" & RowNum).Value = Password
    Next i
    
End Sub
 
Upvote 0
Yea. Adjusting array size dynamically in VBA is impossible I think...
I think in C, you might be able to use a pointer to adjust it dynamically but
in VBA, you have to declare a variable as a constant if you want to use it as an array size.

So, the code you've posted, is it not working cuz of the problem mentioned above?

If not, where is it highlighting when you press Debug?

No, the code I posted works. I just have the array dimensioned for the maximum size.
 
Upvote 0
By the way, for your repetition of 2 characters,
you're only checking adjacent character positions instead of looking at whole string itself.

Your code:
Code:
        'Maximum of 2 repeated characters
        For x = 1 To PasswordLength - 2
            If PasswordArray(x) = PasswordArray(x + 1) Then
                If PasswordArray(x) = PasswordArray(x + 2) Then
                    GoTo GeneratePassword
                End If
            End If
        Next x
 
Upvote 0
By the way, for your repetition of 2 characters,
you're only checking adjacent character positions instead of looking at whole string itself.

Your code:
Code:
        'Maximum of 2 repeated characters
        For x = 1 To PasswordLength - 2
            If PasswordArray(x) = PasswordArray(x + 1) Then
                If PasswordArray(x) = PasswordArray(x + 2) Then
                    GoTo GeneratePassword
                End If
            End If
        Next x
Yes, I know. That's why I changed the code. I only want to check the adjacent characters. Not the entire string.
 
Upvote 0
Another way...

The code starts populating passwords from A2 (assuming A1 is header)<font face=Courier New><SPAN style="color:#00007F">Option</SPAN><SPAN style="color:#00007F">Explicit</SPAN><br><br><SPAN style="color:#00007F">Function</SPAN> GeneratePassword()<br>    <SPAN style="color:#00007F">Dim</SPAN> PassLength<SPAN style="color:#00007F">As</SPAN><SPAN style="color:#00007F">Long</SPAN>, TempPass<SPAN style="color:#00007F">As</SPAN><SPAN style="color:#00007F">String</SPAN>, i<SPAN style="color:#00007F">As</SPAN><SPAN style="color:#00007F">Long</SPAN><br>    PassLength = Int((14 - 8 + 1) * Rnd + 8)<br>    <SPAN style="color:#00007F">For</SPAN> i = 1<SPAN style="color:#00007F">To</SPAN> PassLength<br>        TempPass = Chr(Int((126 - 33 + 1) * Rnd + 33))<br>        GeneratePassword = GeneratePassword & TempPass<br>    <SPAN style="color:#00007F">Next</SPAN> i<br><SPAN style="color:#00007F">End</SPAN><SPAN style="color:#00007F">Function</SPAN><br><br><SPAN style="color:#00007F">Sub</SPAN> RandomPasswordGenerator()<br>    <SPAN style="color:#00007F">Dim</SPAN> Rng<SPAN style="color:#00007F">As</SPAN> Range<br>    <SPAN style="color:#00007F">Dim</SPAN> Password<SPAN style="color:#00007F">As</SPAN><SPAN style="color:#00007F">String</SPAN>, PrevPass<SPAN style="color:#00007F">As</SPAN><SPAN style="color:#00007F">String</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> lCount<SPAN style="color:#00007F">As</SPAN><SPAN style="color:#00007F">Long</SPAN>, RptCount<SPAN style="color:#00007F">As</SPAN><SPAN style="color:#00007F">Long</SPAN>, X<SPAN style="color:#00007F">As</SPAN><SPAN style="color:#00007F">Long</SPAN>, PrevCount<SPAN style="color:#00007F">As</SPAN><SPAN style="color:#00007F">Long</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> oMat1<SPAN style="color:#00007F">As</SPAN><SPAN style="color:#00007F">Object</SPAN>, oMat2<SPAN style="color:#00007F">As</SPAN><SPAN style="color:#00007F">Object</SPAN><br>    <SPAN style="color:#00007F">If</SPAN> Range("A2").Value = ""<SPAN style="color:#00007F">Then</SPAN><br>GenPass1:<br>        RptCount = 0<br>        Password = GeneratePassword<br>        <SPAN style="color:#00007F">Set</SPAN> oMat1 =<SPAN style="color:#00007F">Nothing</SPAN><br>        <SPAN style="color:#00007F">Set</SPAN> oMat2 =<SPAN style="color:#00007F">Nothing</SPAN><br>        <SPAN style="color:#00007F">With</SPAN> CreateObject("VBScript.RegExp")<br>            .Global =<SPAN style="color:#00007F">True</SPAN><br>            .Pattern = "[A-Za-z]"<br>            <SPAN style="color:#00007F">If</SPAN> .test(Password)<SPAN style="color:#00007F">Then</SPAN><br>                <SPAN style="color:#00007F">Set</SPAN> oMat1 = .Execute(Password)<br>            <SPAN style="color:#00007F">End</SPAN><SPAN style="color:#00007F">If</SPAN><br>            .Pattern = "[^A-Za-z]"<br>            <SPAN style="color:#00007F">If</SPAN> .test(Password)<SPAN style="color:#00007F">Then</SPAN><br>                <SPAN style="color:#00007F">Set</SPAN> oMat2 = .Execute(Password)<br>            <SPAN style="color:#00007F">End</SPAN><SPAN style="color:#00007F">If</SPAN><br>            <SPAN style="color:#00007F">For</SPAN> lCount = 1<SPAN style="color:#00007F">To</SPAN> Len(Password) - 2<br>                <SPAN style="color:#00007F">If</SPAN> (Mid(Password, lCount, 1) = Mid(Password, lCount + 1, 1)) And _<br>                (Mid(Password, lCount + 1, 1) = Mid(Password, lCount + 2, 1))<SPAN style="color:#00007F">Then</SPAN><br>                    RptCount = RptCount + 1<br>                <SPAN style="color:#00007F">End</SPAN><SPAN style="color:#00007F">If</SPAN><br>            <SPAN style="color:#00007F">Next</SPAN> lCount<br>            <SPAN style="color:#00007F">If</SPAN> (oMat1.Count >= 3) And (oMat2.Count >= 2) And (RptCount = 0)<SPAN style="color:#00007F">Then</SPAN><br>                Range("A2").Value = Password<br>            <SPAN style="color:#00007F">Else</SPAN><br>                <SPAN style="color:#00007F">GoTo</SPAN> GenPass1<br>            <SPAN style="color:#00007F">End</SPAN><SPAN style="color:#00007F">If</SPAN><br>        <SPAN style="color:#00007F">End</SPAN><SPAN style="color:#00007F">With</SPAN><br>    <SPAN style="color:#00007F">Else</SPAN><br>        lr = Range("A" & Rows.Count).End(xlUp).Row<br>        <SPAN style="color:#00007F">If</SPAN> lr<= 21<SPAN style="color:#00007F">Then</SPAN><br>            <SPAN style="color:#00007F">Set</SPAN> Rng = Range("A2", Range("A" & lr))<br>        <SPAN style="color:#00007F">Else</SPAN><br>            <SPAN style="color:#00007F">Set</SPAN> Rng = Range("A" & Rows.Count).End(xlUp).Offset(-19).Resize(20)<br>        <SPAN style="color:#00007F">End</SPAN><SPAN style="color:#00007F">If</SPAN><br>        PrevPass = Range("A" & lr).Value<br>GenPass2:<br>        RptCount = 0<br>        PrevCount = 0<br>        Password = GeneratePassword<br>        <SPAN style="color:#00007F">Set</SPAN> oMat1 =<SPAN style="color:#00007F">Nothing</SPAN><br>        <SPAN style="color:#00007F">Set</SPAN> oMat2 =<SPAN style="color:#00007F">Nothing</SPAN><br>        <SPAN style="color:#00007F">On</SPAN><SPAN style="color:#00007F">Error</SPAN><SPAN style="color:#00007F">Resume</SPAN><SPAN style="color:#00007F">Next</SPAN><br>        X = WorksheetFunction.Match(Password, Rng, 0)<br>        <SPAN style="color:#00007F">If</SPAN> Err.Number = 0<SPAN style="color:#00007F">Then</SPAN><br>            <SPAN style="color:#00007F">GoTo</SPAN> GenPass2<br>        <SPAN style="color:#00007F">End</SPAN><SPAN style="color:#00007F">If</SPAN><br>        <SPAN style="color:#00007F">With</SPAN> CreateObject("VBScript.RegExp")<br>            .Global =<SPAN style="color:#00007F">True</SPAN><br>            .Pattern = "[A-Za-z]"<br>            <SPAN style="color:#00007F">If</SPAN> .test(Password)<SPAN style="color:#00007F">Then</SPAN><br>                <SPAN style="color:#00007F">Set</SPAN> oMat1 = .Execute(Password)<br>            <SPAN style="color:#00007F">End</SPAN><SPAN style="color:#00007F">If</SPAN><br>            .Pattern = "[^A-Za-z]"<br>            <SPAN style="color:#00007F">If</SPAN> .test(Password)<SPAN style="color:#00007F">Then</SPAN><br>                <SPAN style="color:#00007F">Set</SPAN> oMat2 = .Execute(Password)<br>            <SPAN style="color:#00007F">End</SPAN><SPAN style="color:#00007F">If</SPAN><br>            <SPAN style="color:#00007F">For</SPAN> lCount = 1<SPAN style="color:#00007F">To</SPAN> Len(Password) - 2<br>                <SPAN style="color:#00007F">If</SPAN> (Mid(Password, lCount, 1) = Mid(Password, lCount + 1, 1)) And _<br>                (Mid(Password, lCount + 1, 1) = Mid(Password, lCount + 2, 1))<SPAN style="color:#00007F">Then</SPAN><br>                    RptCount = RptCount + 1<br>                <SPAN style="color:#00007F">End</SPAN><SPAN style="color:#00007F">If</SPAN><br>            <SPAN style="color:#00007F">Next</SPAN> lCount<br>            <SPAN style="color:#00007F">For</SPAN> lCount = 1<SPAN style="color:#00007F">To</SPAN> Len(Password)<br>                <SPAN style="color:#00007F">If</SPAN> InStr(1, PrevPass, Mid(Password, lCount, 1)) = 0<SPAN style="color:#00007F">Then</SPAN><br>                    PrevCount = PrevCount + 1<br>                <SPAN style="color:#00007F">End</SPAN><SPAN style="color:#00007F">If</SPAN><br>            <SPAN style="color:#00007F">Next</SPAN> lCount<br>            <SPAN style="color:#00007F">If</SPAN> (oMat1.Count >= 3) And (oMat2.Count >= 2) And (RptCount = 0) And (PrevCount >= 3)<SPAN style="color:#00007F">Then</SPAN><br>                Range("A" & lr + 1).Value = Password<br>            <SPAN style="color:#00007F">Else</SPAN><br>                <SPAN style="color:#00007F">GoTo</SPAN> GenPass2<br>            <SPAN style="color:#00007F">End</SPAN><SPAN style="color:#00007F">If</SPAN><br>        <SPAN style="color:#00007F">End</SPAN><SPAN style="color:#00007F">With</SPAN><br>    <SPAN style="color:#00007F">End</SPAN><SPAN style="color:#00007F">If</SPAN><br><SPAN style="color:#00007F">End</SPAN><SPAN style="color:#00007F">Sub</SPAN><br></FONT>

Examples of generated passwords.
Excel Workbook
A
1Password List
2SW<=i"hmc%Gr
3D{r&zCRi&XM=[
49;onX}v6b}7S
5
6]GGc?\42W
7Lv9jD
8I*Ubvo#TwI`P
9LBG:&7}&ECO
10M9\S/y^PE
11jLgYo"4'
12@-!S^Tn(
13`KB/cxR)h
14LO4?)X0x*J
15sg:`9)#?k
167N8A%N4rXg
17@T(\G{+w[A/N5~
18d~@OGb1HTmSI
196[O`tC=
20CsM2agZj0l4z
21&kDL,+1%
22SU5LggFug)\d"
23F:}lbHf;BIy
24]A*2(I{S
25|5DF;P.Q{Uv
26Jb'hbO/6?j%Q
27l?|l`vsH,zkbF
2800PG*:]p
292uD?i5K7sZD
30qXxQ@r991A
Sheet1
Excel 2003
 
Upvote 0

Forum statistics

Threads
1,214,987
Messages
6,122,618
Members
449,092
Latest member
amyap

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