PDA

View Full Version : Solved: Generate a random string of letters



Dabo
05-12-2009, 01:16 AM
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

xld
05-12-2009, 01:21 AM
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

Dabo
05-12-2009, 02:16 AM
Great, it works perfectly, my boss will be happy!

Thank you so much XLD

xld
05-12-2009, 02:25 AM
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?

Dabo
05-12-2009, 04:10 AM
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.

xld
05-12-2009, 04:20 AM
What happens if it generates a string you have already used?

Dabo
05-12-2009, 09:11 AM
I put a test to avoid that and generate a new string until it's ok
(even if it is highly unlikely)