🔧
GenerateRandomData
ユーティリティランダムなテストデータを生成する
🎬 デモGIF準備中
📖 使い方
- VBAエディタを開く(Alt + F11)
- モジュールを挿入(挿入 > モジュール)
- 下記VBAコードをコピー&ペースト
- ブック上で実行(Alt + F8 でマクロ選択)
💡 実行例: 実行すると行数・列数・数据类型を選択してテストデータを生成する
💻 VBAコード
' GenerateRandomData
' -----------------
' Macro Name: GenerateRandomData
' Description: ランダムなテストデータを生成する
' Parameters: なし
' Returns: なし
' Usage: 実行すると行数・列数・数据类型を選択してテストデータを生成する
' -----------------
Sub GenerateRandomData()
Dim ws As Worksheet
Dim numRows As Long
Dim numCols As Long
Dim dataType As String
Dim startRow As Long
Dim startCol As Long
Dim i As Long
Dim j As Long
On Error GoTo ErrorHandler
Set ws = ActiveSheet
numRows = InputBox("生成する行数を入力 (1-1000):", "ランダムデータ生成", "10")
If numRows = "" Then Exit Sub
If Not IsNumeric(numRows) Then
MsgBox "数値を入力してください。", vbExclamation
Exit Sub
End If
numRows = CLng(numRows)
If numRows < 1 Or numRows > 1000 Then numRows = 10
numCols = InputBox("生成する列数を入力 (1-20):", "ランダムデータ生成", "5")
If numCols = "" Then Exit Sub
If Not IsNumeric(numCols) Then
MsgBox "数値を入力してください。", vbExclamation
Exit Sub
End If
numCols = CLng(numCols)
If numCols < 1 Or numCols > 20 Then numCols = 5
dataType = InputBox("数据类型を選択:" & vbCrLf & _
"1: 整数 (1-100)" & vbCrLf & _
"2: 小数 (1-100)" & vbCrLf & _
"3: 英字文字列" & vbCrLf & _
"4: 日付", "数据类型選択", "1")
startRow = InputBox("開始行:", "ランダムデータ生成", "1")
If startRow = "" Then startRow = "1"
If Not IsNumeric(startRow) Then startRow = "1"
startRow = CLng(startRow)
startCol = InputBox("開始列:", "ランダムデータ生成", "1")
If startCol = "" Then startCol = "1"
If Not IsNumeric(startCol) Then startCol = "1"
startCol = CLng(startCol)
Randomize
For i = 0 To numRows - 1
For j = 0 To numCols - 1
Select Case dataType
Case "1"
ws.Cells(startRow + i, startCol + j).Value = Int((100 * Rnd) + 1)
Case "2"
ws.Cells(startRow + i, startCol + j).Value = Round(Rnd * 100, 2)
Case "3"
Dim strLen As Long
strLen = Int((10 * Rnd) + 5)
Dim randStr As String
randStr = ""
Dim k As Long
For k = 1 To strLen
randStr = randStr & Chr(Int((122 - 65 + 1) * Rnd) + 65)
Next k
ws.Cells(startRow + i, startCol + j).Value = randStr
Case "4"
Dim startDate As Date
Dim endDate As Date
startDate = DateSerial(2020, 1, 1)
endDate = Date
ws.Cells(startRow + i, startCol + j).Value = startDate + Rnd * (endDate - startDate)
ws.Cells(startRow + i, startCol + j).NumberFormat = "yyyy/mm/dd"
Case Else
ws.Cells(startRow + i, startCol + j).Value = Int((100 * Rnd) + 1)
End Select
Next j
Next i
ws.Columns(ws.Cells(startRow, startCol).Column).AutoFit
MsgBox "ランダムデータを " & numRows & " 行 x " & numCols & " 列 生成しました。", vbInformation
Exit Sub
ErrorHandler:
MsgBox "エラーが発生しました: " & Err.Description, vbCritical
End Sub