如题所述
用下面的VBA代码可以在A1:A100区域中生成由大小写字母和数字组成的12位长度的随机字符串,从中挑选几个作为密码用还是不错的。
在Excel中按Alt+F11打开VBA编辑器,双击“工程”窗口中的某个工作表名称,在右侧的代码窗口中输入下列代码:
Sub MakeRandomString()
Dim J As Integer
Dim K As Integer
Dim iTemp As Integer
Dim sNumber As String
Dim RandomStr(1 To 100, 1 To 1) As String
Dim bOK As Boolean
Randomize
For J = 1 To 100
sNumber = ""
For K = 1 To 12
Do
iTemp = Int((122 – 48 + 1) * Rnd + 48)
Select Case iTemp
Case 48 To 57, 65 To 90, 97 To 122
bOK = True
Case Else
bOK = False
End Select
Loop Until bOK
bOK = False
sNumber = sNumber & Chr(iTemp)
Next K
RandomStr(J, 1) = sNumber
Next J
Range("A1:A100").Value = RandomStr
End Sub
最后运行代码。如果要生成其他长度的随机字符串,将其中的
For K = 1 To 12
中的“12”改为其他数字即可,如:
For K = 1 To 10
即可生成10位长度的随机字符串。
在Excel中按Alt+F11打开VBA编辑器,双击“工程”窗口中的某个工作表名称,在右侧的代码窗口中输入下列代码:
Sub MakeRandomString()
Dim J As Integer
Dim K As Integer
Dim iTemp As Integer
Dim sNumber As String
Dim RandomStr(1 To 100, 1 To 1) As String
Dim bOK As Boolean
Randomize
For J = 1 To 100
sNumber = ""
For K = 1 To 12
Do
iTemp = Int((122 – 48 + 1) * Rnd + 48)
Select Case iTemp
Case 48 To 57, 65 To 90, 97 To 122
bOK = True
Case Else
bOK = False
End Select
Loop Until bOK
bOK = False
sNumber = sNumber & Chr(iTemp)
Next K
RandomStr(J, 1) = sNumber
Next J
Range("A1:A100").Value = RandomStr
End Sub
最后运行代码。如果要生成其他长度的随机字符串,将其中的
For K = 1 To 12
中的“12”改为其他数字即可,如:
For K = 1 To 10
即可生成10位长度的随机字符串。
温馨提示:答案为网友推荐,仅供参考