码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2018-11-27 19:28:49      阅读:536      评论:0      收藏:0      [点我收藏+]

标签:long   开始   next   level   pass   exit   tun   debug   pre   

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

 

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

标签:long   开始   next   level   pass   exit   tun   debug   pre   

原文地址:https://www.cnblogs.com/dunkbird/p/10027808.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!