Random Text Generator

This function will return random text (a-z) for the specified length. For ex. Randtxt(5) will return xythp.

‘—————————————————————————————
‘ Procedure : Randtxt
‘ DateTime  : 10/23/2005 11:54
‘ Author    : Ashutosh
‘ Purpose   :This function will return random text for the specified no. of chars

Function Randtxt(nochars As Byte) As String
    On Error GoTo Randtxt_Error
    If nochars = 0 Then Exit Function
    Dim sResult As String, i As Long
        sResult = “”
        For i = 1 To nochars
           sResult = sResult & Chr(Int(Rnd * (122 – 97) + 97))
        Next i
    Randtxt = sResult
    Exit Function
Randtxt_Error:
    MsgBox “Error ” & Err.Number & ” (” & Err.Description & “) in function Randtxt”
End Function

This sub will return random text for the specified length and number of random texts needed.

‘ Procedure : Randtxt1
‘ DateTime  : 10/23/2005 12:12
‘ Author    : Ashutosh
‘ Purpose   : This sub will return and no. of random text random text for the
‘specified no. of chars needed starting from the activecell
Sub Randtxt1()
Dim nochars As Byte
Dim nos As Long
nochars = InputBox(“Enter the length of the random text”, , 1)
nos = InputBox(“Enter the number the random texts needed”, , 1)
    On Error GoTo Randtxt_Error1
    If nochars = 0 Then Exit Sub
    Dim sResult As String, i As Long, j As Long
    For j = 1 To nos
        sResult = “”
        For i = 1 To nochars
           sResult = sResult & Chr(Int(Rnd * (122 – 97) + 97))
        Next i
            ActiveCell.Offset(j – 1, 0) = sResult
    Next j
    Exit Sub
Randtxt_Error1:
    MsgBox “Error ” & Err.Number & ” (” & Err.Description & _
        “) in procedure Randtxt”
End Sub

About the Author

A co-author of Data Science for Fundraising, an award winning keynote speaker, Ashutosh R. Nandeshwar is one of the few analytics professionals in the higher education industry who has developed analytical solutions for all stages of the student life cycle (from recruitment to giving). He enjoys speaking about the power of data, as well as ranting about data professionals who chase after “interesting” things. He earned his PhD/MS from West Virginia University and his BEng from Nagpur University, all in industrial engineering. Currently, he is leading the data science, reporting, and prospect development efforts at the University of Southern California.

  • Jason says:

    This is a great little script. However, it needs to be incremented in the range to 26 letters so Z’s will appear.

    Others may also appreciate an option with capital letters.

  • Filo Kirk says:

    I used this for a cell but the text wasn’t long enough so I used concatenate to make the string longer…=CONCATENATE(randtxt(200),randtxt(200),randtxt(200),randtxt(200),randtxt(200))

  • >