+ Reply to Thread
Results 1 to 5 of 5

Random character/password

Hybrid View

rx123 Random character/password 06-27-2009, 10:20 AM
Keyur Re: Random character/password 06-28-2009, 10:04 AM
shg Re: Random character/password 06-28-2009, 10:19 AM
Keyur Re: Random character/password 06-28-2009, 02:18 PM
shg Re: Random character/password 06-28-2009, 05:57 PM
  1. #1
    Registered User
    Join Date
    06-27-2009
    Location
    austrlia
    MS-Off Ver
    Excel 2003
    Posts
    1

    Random character/password

    Random password

    I used the following code to generate random password from the cell name list containing aAbcdefgh…12345 etc.

    = (MID(list,1+INT(RAND()*LEN(list)),1))

    however the problem is each time I enter a text somewhere else in the worksheet and press enter, the above function gets activated automatically and the password keeps changing. Is there a way I can stop and start the function when I want to?

    For example, say I want to have a list of forum such as exel forum, java forum, .net forum and have the corresponding password automatically generated just once until I change it later if I want to. The current problem is each time I try to type in a new forum name such java forum, all the old passwords change automatically. How can I stop this. Can I use buttons or what ever ?

  2. #2
    Registered User
    Join Date
    03-31-2004
    Location
    Toronto, Canada
    MS-Off Ver
    2003/2007
    Posts
    36

    Re: Random character/password

    Hey,
    Open your file and click Alt+F11. VBA editor opens.
    Goto Insert/Module and a new Module1 will open. Copy Paste the code below.

    Sub generate_password()
    Dim lst As String, pass As String
    Dim pass_len As Integer
    lst = "abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    pass_len = 8
    For Each cll In Application.Selection
        pass = ""
        For i = 1 To pass_len
            pass = pass & Mid(lst, Int(Rnd() * Len(lst)), 1)
        Next
        cll.Value = pass
    Next
    End Sub
    Add a button on the sheet (or a textbox). right click on it and select 'Assign Macro'. select the macro we added and hit OK.

    so you can either just select a single cell or a bunch of cells and hit the button to generate random passwords.

    to change the length of your password modify the line 'pass_len = 8'.

    hope this is what you were looking for.
    Cheers

  3. #3
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Random character/password

    pass = pass & Mid(lst, Int(Rnd() * Len(lst)) + 1, 1)
    Entia non sunt multiplicanda sine necessitate

  4. #4
    Registered User
    Join Date
    03-31-2004
    Location
    Toronto, Canada
    MS-Off Ver
    2003/2007
    Posts
    36

    Re: Random character/password

    Hi Shg,
    When I tried my code again I did get the error the first time and then it worked fine. could you please explain what '+1' does? Thanks.

  5. #5
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Random character/password

    Rnd returns a value >=0 and <1; so int(rnd*n) always returns a value between 0 and n-1. You want a value between 1 and n. Without the +1, you'd never use the last character in the string, and sometimes get an error.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1