【VBA】入力チェック処理のサンプル

PR

指定セルに入力されたフォルダパスの入力必須チェック

'フォルダの入力必須チェック
If Range("B3").Value = "" Then
    MsgBox "フォルダが入力されていません"
    Exit Sub
End If
PR

指定セルに入力されたフォルダパスの存在チェック

Dim isPathExist As String
'フォルダの存在チェック
isPathExist = Dir(Range("B3").Value, vbDirectory)
If isPathExist = "" Then
    MsgBox "フォルダが存在しません"
    Exit Sub
End If
PR

指定セルに入力されたファイルパスの存在チェック

Dim isPathExist As String
'ファイルの存在チェック
isPathExist = Dir(Range("B3").Value, vbNormal)
If isPathExist = "" Then
    MsgBox "ファイルが存在しません"
    Exit Sub
End If
PR

指定セルに入力されたフォルダ配下に作成するテキストファイルの開閉チェック

Dim isPathExist As String
Dim outputFilePath As String

outputFilePath = Range("B3").Value & "\test.txt"

'ファイルの存在チェック
isPathExist = Dir(outputFilePath, vbNormal)
If isPathExist <> "" Then
    'ファイルが存在した場合、ファイル開閉チェック
    If isFileOpen(outputFilePath) Then
        MsgBox "test.txtが開いています。閉じてから再実行してください"
        Exit Sub
    End If
End If

'ファイル開閉チェック
Private Function isFileOpen(ByVal strArgFile As String) As Boolean

    On Error GoTo FILE_ERR
    
    Open strArgFile For Binary Access Read Lock Read As #1
    Close #1
    isFileOpen = False
    Exit Function
    
FILE_ERR:
    isFileOpen = True   
End Function
タイトルとURLをコピーしました