Skip to content

Instantly share code, notes, and snippets.

@furyutei
Last active April 22, 2020 01:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save furyutei/7ec2ca59611b0d42b440a8fdeb8be54b to your computer and use it in GitHub Desktop.
Save furyutei/7ec2ca59611b0d42b440a8fdeb8be54b to your computer and use it in GitHub Desktop.
パスワードジェネレータを作る
Option Explicit
Sub GeneratePassword()
'【1】生成するパスワードの桁数を設定
Dim passwordLength As Long
passwordLength = Application.InputBox("何桁のパスワードを生成しますか?(※8桁以上必要です)", Default:=8, Type:=1)
' ※ Application.InputBox の Type に 1 を指定することにより、数値に限定
' https://msdn.microsoft.com/en-us/vba/excel-vba/articles/application-inputbox-method-excel
If passwordLength < 8 Then
MsgBox "8以上の数字を入力してください", Buttons:=vbExclamation
Exit Sub
End If
Dim password As String
password = GeneratePasswordString(passwordLength)
'※パスワード生成のコア部分を切り離すことで、汎用性を高める
'【6】出力
Debug.Print passwordLength & "桁のパスワード生成しました。" & password
End Sub
Function GeneratePasswordString(Optional ByVal passwordLength As Long = 8) As String
'機能:passwordLength で指定された長さのパスワードを返す
'※ エラー発生時は vbNullStringを返す
'TODO: より汎用性を高めるため、候補文字や必須文字もオプションで指定できるようにしたいところ
On Error GoTo Failure
'【2-1】パスワード候補の文字列を設定
Dim char1 As String, char2 As String, char3 As String, char4 As String
char1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
char2 = "abcdefghijklmnopqrstuvwxyz"
char3 = "0123456789"
char4 = "!@#$%^&*_+-?"
Dim charAll As String: charAll = char1 & char2 & char3 & char4
'【2-2】パスワード文字列を格納する文字列を準備
Dim sourcePassword As String
'【3】必須文字の取得(大文字・小文字・数字・記号を最低1文字以上含ませるため)
sourcePassword = GetRndChar(char1) & GetRndChar(char2) & GetRndChar(char3) & GetRndChar(char4)
'【4】残りの桁数分を、すべての候補文字からランダムに取得
While Len(sourcePassword) < passwordLength
sourcePassword = sourcePassword & GetRndChar(charAll)
Wend
'【5】要素番号0~3に規則性ができてしまうため、全体を適当にシャッフル
GeneratePasswordString = ShuffleString(sourcePassword)
Exit Function
Failure:
GeneratePasswordString = vbNullString
End Function
Function GetRndChar(ByRef sourceString As String, Optional ByRef position As Long) As String
'機能:受け取った文字列からランダムに1文字抽出して返す
'※引数 position が渡された場合、position には選択した文字の位置が返る
position = WorksheetFunction.RandBetween(1, Len(sourceString))
GetRndChar = Mid(sourceString, position, 1)
End Function
Function ShuffleString(ByRef sourceString As String) As String
'機能: 受け取った文字列を適当に並び替えて返す
Dim remainString As String: remainString = sourceString
Dim position As Long
While 0 < Len(remainString)
' 残りの文字列からランダムに1文字抽出
ShuffleString = ShuffleString & GetRndChar(remainString, position)
' 残りの文字列から抽出した文字を除去
remainString = Left(remainString, position - 1) & Right(remainString, Len(remainString) - position)
Wend
End Function
@furyutei
Copy link
Author

furyutei commented Aug 1, 2018

元ネタ:【ノンプロ研_お題】VBAでパスワードジェネレータを作ってみた(ランダムな文字列を作成) - IT女子がお届けするオフィスワーク効率化・VBA技術紹介

文字列をランダムに並び替える処理をもう少しシンプルに書けるかな? と思ったので、試行。
※速度的にどうかは知らない。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment