PDA

View Full Version : Solved: Insert dash in password



av8tordude
07-28-2011, 05:52 PM
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

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

mikerickson
07-28-2011, 07:43 PM
You could use this UDF

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

av8tordude
07-28-2011, 08:10 PM
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?

mikerickson
07-28-2011, 10:38 PM
The logic would be something like

Sub Main()
' create password
finalPassword = InsertRandomDash(createdPassword, 3)
End Sub

av8tordude
07-29-2011, 09:31 AM
Thank you Mike. It works great!:friends: