excel macro実験


```ruby:excelmacro実験

Option Explicit

Private Sub CommandButton1_Click()
Dim min As Long
Dim max As Long

    On Error GoTo errorHandler
    
    Randomize
    
    Debug.Print "1回目:" & Application.WorksheetFunction.RoundDown(CCur(Rnd * 10000000000#), 0)
    Debug.Print "2回目:" & Application.WorksheetFunction.RoundDown(CCur(Rnd * 10000000000#), 0)
    Debug.Print "3回目:" & Application.WorksheetFunction.RoundDown(CCur(Rnd * 10000000000#), 0)
    Debug.Print "4回目:" & Application.WorksheetFunction.RoundDown(CCur(Rnd * 10000000000#), 0)
    Debug.Print "5回目:" & Application.WorksheetFunction.RoundDown(CCur(Rnd * 10000000000#), 0)
    
    Exit Sub

errorHandler:
    MsgBox "エラー番号:" & Err.Number & vbCrLf & _
        "エラー内容:" & Err.Description
End Sub

Private Sub CommandButton2_Click()
Dim strArray() As String
Dim result As String
Dim id As String
Dim i As Long

    On Error GoTo errorHandler
    
    id = ActiveSheet.Range("B5").Value
    
    If id = "MST00001" Then
        ReDim strArray(4, 1) As String
        strArray(0, 0) = "MST00001.csv"
        strArray(0, 1) = "SELECT * FROM MST00001"
        strArray(1, 0) = "MST00002.csv"
        strArray(1, 1) = "SELECT * FROM MST00002"
        strArray(2, 0) = "MST00003.csv"
        strArray(2, 1) = "SELECT * FROM MST00003"
        strArray(3, 0) = "MST00004.csv"
        strArray(3, 1) = "SELECT * FROM MST00004"
        strArray(4, 0) = "MST00005.csv"
        strArray(4, 1) = "SELECT * FROM MST00005"
    ElseIf id = "TBL00001" Then
        ReDim strArray(2, 1) As String
        strArray(0, 0) = "TBL00001.csv"
        strArray(0, 1) = "SELECT * FROM TBL00001"
        strArray(1, 0) = "TBL00002.csv"
        strArray(1, 1) = "SELECT * FROM TBL00002"
        strArray(2, 0) = "TBL00003.csv"
        strArray(2, 1) = "SELECT * FROM TBL00003"
    ElseIf id = "TBL00002" Then
        ReDim strArray(3, 1) As String
        strArray(0, 0) = "TBL00001.csv"
        strArray(0, 1) = "SELECT * FROM TBL00002"
        strArray(1, 0) = "TBL00002.csv"
        strArray(1, 1) = "SELECT * FROM TBL00003"
        strArray(2, 0) = "TBL00003.csv"
        strArray(2, 1) = "SELECT * FROM TBL00004"
        strArray(3, 0) = "TBL00004.csv"
        strArray(3, 1) = "SELECT * FROM TBL00005"
    Else
        GoTo errorHandler
    End If

    For i = 0 To UBound(strArray, 1)
        result = ArrayFunc(strArray(i, 0), strArray(i, 1), i)
        MsgBox result
    Next i

    Erase strArray
    Exit Sub

errorHandler:
    MsgBox "エラーが発生しました。"
    Erase strArray
End Sub

Function ArrayFunc(fileName As String, strSql As String, idx As Long) As String

    ArrayFunc = "idx=" & idx & ", " & fileName & "," & strSql
    
End Function

Private Sub CommandButton3_Click()
Dim fso As Object
Dim Target As String
Set fso = CreateObject("Scripting.FileSystemObject") ' インスタンス化
    
    On Error GoTo errorHandler

    Target = ActiveSheet.Range("B12").Value
    
    If fso.FileExists(Target) = False Then
        MsgBox Target & "が存在しません。"
        Exit Sub
    End If
    
    Call fso.DeleteFile(Target, True) ' 指定したパスのファイルを削除
    ' 後始末
    Set fso = Nothing
    
    MsgBox Target & "ファイルを削除しました。"
    
    Exit Sub

errorHandler:
    MsgBox "エラー番号:" & Err.Number & vbCrLf & _
        "エラー内容:" & Err.Description
End Sub
```