我白忙了,就是字符串和数字连接问题啊, 用 & 连接符。
不过已经忙了就附上吧:
'主函数
Function GetRndString() As String
Randomize
Dim item As String
item = GetRndLetters(Int(Rnd * 10 + 1)) '获取长度1 到10随机确定的随机字母串
item = item & GetRndDigits(Int(Rnd * 10 + 1)) '获取长度1到10随机确定的随机数字串
GetRndString = item
End Function
'产生给定数量的随机数字字符串,由GetRndString调用
Function GetRndDigits(nlen As Integer) As String
Dim item As String
Dim i As Integer
Randomize
For i = 1 To nlen
item = item & Int(Rnd * 10)
Next
GetRndDigits = item
End Function
'产生给定数量的随机字母字符串,由GetRndString调用
Function GetRndLetters(nlen As Integer) As String
Const CTable = "ABCDEFGHIJKLMNOPQRSTUVWXYZzbcdefghijklmnopqrstuvwxyz" '如果只想产生大写字母的话,那就把小写字母删除掉
Dim item As String
Dim i As Integer
Dim loc As Integer
Randomize
For i = 1 To nlen
loc = Int(Rnd * Len(CTable) + 1)
item = item & Mid(CTable, loc, 1)
Next
GetRndLetters = item
End Function
参考一下这个:
Private Sub Command1_Click()
Dim strText$, i&, v&
strText = vbNullString
Randomize
For i = 1 To 8 ' 1 to 8 就是生成长度为8字符
v = Rnd() * 35
If (v > 9) Then
strText = strText & Chr$(55 + v)
Else
strText = strText & Chr$(48 + v)
End If
Next
MsgBox strText, vbInformation
End Sub
字母和数字的格式不统一,你要先把数字变为字符,再加在一起就可以了。
杜守龙说,这段代码可以生成数字与大小写字母混合的10个字节的字符串。
Private Sub Command1_Click()
Dim s As String
Dim a(1 To 3) As String
Dim i As Integer
Dim c As Long
a(1) = Chr(Asc("0") + Int(Rnd() * 9))
a(2) = Chr(Asc("a") + Int(Rnd() * 26))
a(3) = Chr(Asc("A") + Int(Rnd() * 26))
c = Len("s")
Do Until c >= 11
Randomize Timer
s = s & a(Int(Rnd(Rnd()) * 3 + 1))
c = c + 1
Loop
Print s
End Sub