In VBA `Rnd` is a function, not a class and strings can't be treated as arrays. You need to use the function `Mid` to extract individual characters.
You can write a function to return a random string. The function can then be used by your event-handler:
Function RandString(n As Long) As String
'Assumes that Randomize has been invoked by caller
Dim i As Long, j As Long, m As Long, s As String, pool As String
pool = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
m = Len(pool)
For i = 1 To n
j = 1 + Int(m * Rnd())
s = s & Mid(pool, j, 1)
Next i
RandString = s
RandString = Asc(RandString)
End Function
Sub test()
Randomize
MsgBox RandString(50)
End Sub
Typical output looks like:
fvdDUV1csFLhzCmrvJtYx4wXr1QGqSai6yiGSC4ByzB53kG5E1
Then to convert your string to number :
Asc(myString)
So the output of the example will be : `102`
Hope this helped you
**NOTE :** You can add to pool all char you Want (for your example : "-" or " " )
In VBA `Rnd` is a function, not a class and strings can't be treated as arrays. You need to use the function `Mid` to extract individual characters.
You can write a function to return a random string. The function can then be used by your event-handler:
Function RandString(n As Long) As String
'Assumes that Randomize has been invoked by caller
Dim i As Long, j As Long, m As Long, s As String, pool As String
pool = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
m = Len(pool)
For i = 1 To n
j = 1 + Int(m * Rnd())
s = s & Mid(pool, j, 1)
Next i
RandString = s
End Function
Used like this:
Sub test()
Randomize
MsgBox RandString(50)
End Sub
Typical output looks like:
fvdDUV1csFLhzCmrvJtYx4wXr1QGqSai6yiGSC4ByzB53kG5E1