vba code that generate 3 random letters (1 Viewer)

mjs

New member
Local time
Tomorrow, 06:48
Joined
Nov 26, 2008
Messages
4
Can anyone help me how to write a function that will generate a 3 random letters


Thank you in advanced :)
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 21:48
Joined
Sep 12, 2006
Messages
15,662
just rnd gives you a random nuber less than 1

so cint(rnd*26)+1 will give you a whole (integer) random number in the range 1 to 26

ascii codes for A to Z are 65 to 90

so

chr(cint(rnd * 26) + 65)

should give you one random character in the range A to Z
 

vicsar

https://about.me/vicsar
Local time
Today, 14:48
Joined
Jul 25, 2012
Messages
35
I took some inspiration from this thread and here is my approach to this task. With creativity you can use it in fields for unique identifiers (my case) or ... who knows, be creative.

Hopefully someone can use it some how:

Code:
   Sub Generate_Random_Serial()
   Dim lngUnique_Random_Number_One As Long
   Dim lngUnique_Random_Number_Two As Long
   Dim stUnique_Random_Letter_One As String
   Dim stUnique_Random_Letter_Two As String
   
   Dim strRandomly_Generated_Serial As String
   
   ' Reset the random seed.
       Rnd -1
   ' Initialize the seed using Timer.
       Randomize (Timer)
   
   'Randomly choose a number from 1 to 8,923,875
       lngUnique_Random_Number_One = Int((8923875 - 1 + 1) * Rnd() + 1)
       lngUnique_Random_Number_Two = Int((8923875 - 1 + 1) * Rnd() + 1)
   
   'Randomly choose a character (letter)
       stUnique_Random_Letter_One = chr(CInt(Rnd * 26) + 65)
       stUnique_Random_Letter_Two = chr(CInt(Rnd * 26) + 65)
   
   'Concatenate and build the serial
       strRandomly_Generated_Serial = _
           lngUnique_Random_Number_One _
           & stUnique_Random_Letter_One _
           & lngUnique_Random_Number_Two _
           & stUnique_Random_Letter_Two
   
   MsgBox "Requested serial is: " & strRandomly_Generated_Serial, vbInformation, strRandomly_Generated_Serial
   
   End Sub
 

Users who are viewing this thread

Top Bottom