Vinod Chelladurai
Vinod Chelladurai

Reputation: 539

Excel vba to check if a workbook has opened

IS there any way to check if an excel file is opened or not? If it is opened, then how can i close it?

thanks in advance

Upvotes: 0

Views: 181

Answers (1)

Gary's Student
Gary's Student

Reputation: 96791

If the workbook name does not include the period character, it can be as simple as:

Sub TestForOpen()
    Dim wb As Workbook, st As String
    st = "Phone"
    For Each wb In Workbooks
        stwb = Split(wb.Name, ".")(0)
        If st = stwb Then
            wb.Activate
            ActiveWorkbook.Close
            Exit Sub
        End If
    Next wb
End Sub

This one looks for an open workbook named Phone.xls or Phone.xlsx or ..................

If found the workbook is closed.

Upvotes: 1

Related Questions