用下面的VBA代碼可以在A1:A100區(qū)域中生成由大小寫字母和數(shù)字組成的12位長度的隨機字符串,從中挑選幾個作為密碼用還是不錯的。
在Excel中按Alt+F11打開VBA編輯器,雙擊“工程”窗口中的某個工作表名稱,在右側(cè)的代碼窗口中輸入下列代碼:
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”改為其他數(shù)字即可,如:
For K = 1 To 10
即可生成10位長度的隨機字符串。