VBA 生成带时间戳的随机数字

Function GenPasswd(length, level)
Dim allstr, substr, passwd As String

allstr = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()"
Select Case level
Case 1
strlen = 10
Case 2
strlen = 36
Case 3
strlen = 62
Case Else
strlen = 72
End Select
substr = Left(allstr, strlen)
'Debug.Print substr
passwd = ""
For i = 1 To length
passwd = passwd & Mid(substr, Int(Rnd * strlen + 1), 1)
Next
GenPasswd = passwd
End Function
Function GetUnixTime()
GetUnixTime = DateDiff("s", "01/01/1970 00:00:00", Now())
End Function
Function GeneratePassData(strlen As Integer)

Dim str As String

str = Convert10To62(GetUnixTime())
If strlen <= Len(str) Then
GeneratePassData = str
Exit Function
End If
GeneratePassData = GenPasswd(strlen - Len(str), 3) + str

End Function

Function Convert10To62(str As Long)

Dim i, j As Integer
Dim rest As Long
Dim arr()
Dim str1 As String
Dim retStr As String

retStr = ""
rest = str
str1 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
ReDim Preserve arr(Len(str1))
For i = 1 To Len(str1)
arr(i) = Mid(str1, i, 1)
Next

While rest <> 0
j = rest - (rest \ 62) * 62 + 1 'vba 角标是从1开始,不是从0开始
retStr = retStr + arr(j)
rest = rest \ 62
Wend
Convert10To62 = StrReverse(retStr)
End Function

 

posted @ 2018-11-27 17:34  dunkbird  阅读(1082)  评论(0编辑  收藏  举报