VBA Tip: Generate codes randomly


Here is the code to generate a random code of 10 characters:

Sub random_code()

    'www.excel-pratique.com/en/vba_tricks/generate-code-randomly.php
    Randomize
 
    char_list = "ABCD1234"
    code_gen = ""
 
    For i = 1 To 10 '10 = code length
        random_number = Int(Len(char_list) * Rnd) + 1
        code_gen = code_gen & Mid(char_list, random_number, 1)
    Next
 
    MsgBox code_gen
   
End Sub