suin.io

VBAでランダムな文字列を生成する関数

suin2008年12月15日

ランダムな文字列を生成する関数です。エクセルなどでパスワードを生成するなどの用途に用います。

Public Function pass(Optional intLength As Integer = 8, Optional boolIncUpper As Boolean = False, Optional boolIncSign As Boolean = False)

    Dim strPass As String
    Dim strLower As String
    Dim strUpper As String
    Dim strNumber As String
    Dim strSign As String
    Dim strChars As String
    Dim intMax As Integer
    Dim i As Integer

    strLower = "qwertyuiopasdfghjklzxcvbnm"
    strUpper = "QWERTYUIOPASDFGHJKLZXCVBNM"
    strNumber = "123456789"
    strSign = "-^@[;:],./!""#$%&'()=~|`{+*}<>?_"

    strChars = strLower & strNumber

    If boolIncUpper = True Then

        strChars = strChars & strUpper

    End If

    If boolIncSign = True Then

        strChars = strChars & strSign

    End If

    intMax = Len(strChars)

    For i = 1 To intLength

        strPass = Mid(strChars, Int((intMax - 1 + 1) * Rnd + 1), 1) & strPass

    Next i

    pass = strPass

End Function
RELATED POSTS