Consulting

Results 1 to 7 of 7

Thread: Solved: Generate a random string of letters

  1. #1
    VBAX Regular
    Joined
    Apr 2009
    Posts
    36
    Location

    Question Solved: Generate a random string of letters

    Hello everybody,

    I have to generate a string of letters of this kind:

    M + 7 letters

    for example :

    MAZERTYU
    MPOISURE

    etc

    What kind of code could I use ?

    Thanks

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master xld's Avatar
    Joined
    Apr 2005
    Posts
    25,149
    Location
    [vba]

    Dim num As Long
    Dim tmp As String
    Dim i As Long

    tmp = "M"
    For i = 1 To 7

    tmp = tmp & Chr(64 + Int((Rnd() * 26) + 1))
    Next i
    Debug.Print tmp
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Regular
    Joined
    Apr 2009
    Posts
    36
    Location
    Great, it works perfectly, my boss will be happy!

    Thank you so much XLD

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master xld's Avatar
    Joined
    Apr 2005
    Posts
    25,149
    Location
    Quote Originally Posted by Dabo
    Great, it works perfectly, my boss will be happy!

    Thank you so much XLD
    That's okay. But can I ask what possible use you have for this code, I am intrigued?
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    VBAX Regular
    Joined
    Apr 2009
    Posts
    36
    Location
    Sure,

    I have a list of Ids for customers
    "M0000001" = customer 1
    "M0000002" = customer 2
    etc
    I need to let the user add new customers.

    When he doesn't know the Id of the customer, I need to generate a new one with letters instead of figures, to be able to recognize them.

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master xld's Avatar
    Joined
    Apr 2005
    Posts
    25,149
    Location
    What happens if it generates a string you have already used?
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  7. #7
    VBAX Regular
    Joined
    Apr 2009
    Posts
    36
    Location
    I put a test to avoid that and generate a new string until it's ok
    (even if it is highly unlikely)

Posting Permissions

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