VBA ブックが開かれているか確認

実行用プロシージャ

Sub Sample()
    Dim fName As String
    fName = "test.xlsx"
    If IsOpenBook(fName) Then
        '処理
    End If
End Sub

処理用プロシージャ

Function IsOpenBook(ByVal bookName As String) As Boolean
    Dim res As Boolean
    Dim wb As Workbook
     
    For Each wb In Workbooks
        If wb.Name = bookName Then
            res = True
            Exit For
        End If
    Next
     
    IsOpenBook = res
End Function

コメント