Consulting

Results 1 to 5 of 5

Thread: Solved: Insert dash in password

  1. #1

    Solved: Insert dash in password

    Hi Everyone, Can someone assist with inserting 3 dashes (in random position) of the password created.

    Thanks

    e.g.
    jOov-PkA-5YXwF-eU1x
    ZmFE-A5o2-TiwU-ziYa
    E2-zz4ZX-cBfbXM-p2b

    [vba]Sub PasswordGenerator()
    Dim Password As String
    Dim PasswordLength As Byte
    Dim LC As Byte 'Loop Counter
    Dim strRndmChr As String
    Dim LAC As Byte 'Lowest Ascii Character
    Dim HAC As Byte 'Highest Ascii Character
    Dim UseSymbolics As Boolean
    Dim HasSymbolics As Boolean
    Dim RandomNumber As Byte

    'Set parameters.
    PasswordLength = 16

    LAC = Asc("0")
    HAC = Asc("z")
    UseSymbolics = False

    Randomize
    For LC = 1 To PasswordLength
    'To produce random integers in a given range, use this formula:
    'Int((upperbound - lowerbound + 1) * Rnd + lowerbound)
    Do
    RandomNumber = Int((HAC - LAC + 1) * Rnd + LAC)
    strRndmChr = Chr(RandomNumber)
    HasSymbolics = CheckSymbolics(RandomNumber)
    Loop Until UseSymbolics = True Or HasSymbolics = False
    Password = Password & strRndmChr
    Next LC
    Range("D2").Value = Password
    'ActiveCell = Password

    End Sub
    Private Function CheckSymbolics(RandomNumber As Byte)
    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 _
    CheckSymbolics = True Else: CheckSymbolics = False
    End Function

    [/vba]

  2. #2
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    You could use this UDF

    [VBA]Function InsertRandomDash(aString As String, numberOfDashes As String)
    Dim i As Long, Size As Long, randindex As Long
    Dim temp As String
    Dim dashArray() As String
    Size = Len(aString) - 1
    If Size < numberOfDashes Then numberOfDashes = Size
    If 0 < Size Then
    ReDim dashArray(1 To Size)
    For i = 1 To numberOfDashes
    dashArray(i) = "-"
    Next i
    For i = 1 To numberOfDashes
    randindex = Int(Rnd() * Size) + 1
    temp = dashArray(randindex)
    dashArray(randindex) = dashArray(i)
    dashArray(i) = temp
    Next i
    For i = 1 To Len(aString) - 1
    InsertRandomDash = InsertRandomDash & Mid(aString, i, 1) & dashArray(i)
    Next i
    InsertRandomDash = InsertRandomDash & Right(aString, 1)
    End If
    End Function[/VBA]

  3. #3
    Hi Mike,

    Thank you for the code, but I do I integrate your code? Also, If i wanted to in the password characters, where do i adjust the number of dashes?

  4. #4
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    The logic would be something like
    [vba]
    Sub Main()
    ' create password
    finalPassword = InsertRandomDash(createdPassword, 3)
    End Sub[/vba]

  5. #5
    Thank you Mike. It works great!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •