Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Checking For An Open Workbook

Status
Not open for further replies.

SandraF

Programmer
Jan 25, 2001
28
US
I use the following code to open up a workbook in a macro that I created:

Workbooks.Open "E:\DBTest.xls"
Set wbTemp = ActiveWorkbook

The problem is that if the workbook is already opened, an error occurs. IS there a way to use an IF statement so that if an error occurs, I send a message to the user saying that the workbook is already opened (MsgBox "The Workbook is already opened - Try again later!") and then it stops the macro from running?

THanks!!!!!!
 
Assuming that you only have one instance of Excel running:
Code:
Public Sub Test()
    If IsAlreadyOpen("DBTest.xls") = True Then
        MsgBox "Already Opened!"
    Else
        MsgBox "Not Opened Yet!"
    End If
End Sub

Public Function IsAlreadyOpen(sName As String) As Boolean
    Dim wbTmp As Workbook
    For Each wbTmp In Application.Workbooks
        If UCase(wbTmp.Name) Like UCase(sName) Then
            IsAlreadyOpen = True
        Else
            IsAlreadyOpen = False
        End If
    Next
    Set wbTmp = Nothing
End Function

Hope this helps! DimensionalSolutions@Core.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top