Thread: [Solved] New to VBA
View Single Post
 
Old 04-07-2017, 09:55 AM
DeductiveBear23 DeductiveBear23 is offline Windows 10 Office 2016
Novice
 
Join Date: Apr 2017
Posts: 4
DeductiveBear23 is on a distinguished road
Unhappy New to VBA

Hello All,

Very new to the entire world of VBA and Macros. I started with a want to add a random password generator to Word document. Some other people wanted the same thing and they were provided with some code that works exactly how I would like to it to.
This being said, I would like the randomized string of characters to appear in a table titled 'Password' instead of the message box. The code I am using is:
Code:
Sub PASSWORD_GENERATOR()
MsgBox Pwd(8)
End Sub
 
Function Pwd(iLength As Integer) As String
Dim i As Integer, iTemp As Integer, bOK As Boolean, strTemp As String
'48-57 = 0 To 9, 65-90 = A To Z, 97-122 = a To z
'amend For other characters If required
For i = 1 To iLength
  Do
    iTemp = Int((122 - 48 + 1) * Rnd + 48)
    Select Case iTemp
      Case 48 To 57, 65 To 90, 97 To 122: bOK = True
      Case Else: bOK = False
    End Select
  Loop Until bOK = True
  bOK = False
  strTemp = strTemp & Chr(iTemp)
Next i
Pwd = strTemp
End Function
I was able to produce the desired result in an Excel cell by changing the second last line where 'Pwd = strTemp' to ActiveSheet.Range("C2").Formula = pw (C2 being any cell I need it to be). I have tried "ActivTable.Range ("Password") Formula = pw" but I always get a 424 error and am not sure what or where to place the missing 'Object'.

As always - Any and all assistance is appreciated - Thank you

Last edited by macropod; 05-02-2017 at 03:56 PM. Reason: Added code tags & formatting
Reply With Quote