Workbook Activate Or Open

John Davis

Well-known Member
Joined
Sep 11, 2007
Messages
3,457
Hello All:

I have a workbook which I use alot. Sometimes it is already Open. However sometimes it is Closed and filed away. I have a macro which is designed too run when the workbook is already open. Is there code which I could use to say if the workbook is Open then activate and run the macro, else Open the workbook and run the macro. I think this can be done, I believe it would require an if statement. I just don't know how to construct this code. Any help would be appreciated.
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Code:
Function IsFileOpen(FileName As String)
Dim iFilenum As Long
Dim iErr As Long

    On Error Resume Next
    iFilenum = FreeFile()
    Open FileName For Input Lock Read As #iFilenum
    Close iFilenum
    iErr = Err
    On Error GoTo 0

    Select Case iErr
        Case 0:    IsFileOpen = False
        Case 70:   IsFileOpen = True
        Case Else: Error iErr
    End Select
    
End Function

Sub test()
    If Not IsFileOpen("C:\MyTest\volker2.xls") Then
        Workbooks.Open "C:\MyTest\volker2.xls"
    End If
End Sub
 
Upvote 0
Code:
Function IsFileOpen(FileName As String)
Dim iFilenum As Long
Dim iErr As Long

    On Error Resume Next
    iFilenum = FreeFile()
    Open FileName For Input Lock Read As #iFilenum
    Close iFilenum
    iErr = Err
    On Error GoTo 0

    Select Case iErr
        Case 0:    IsFileOpen = False
        Case 70:   IsFileOpen = True
        Case Else: Error iErr
    End Select
    
End Function

Sub test()
    If Not IsFileOpen("C:\MyTest\volker2.xls") Then
        Workbooks.Open "C:\MyTest\volker2.xls"
    End If
End Sub

Thanks alot XLD. Works perfectly. BTW where is Wessex located on the map? Is it a country? Which continent?
 
Upvote 0
Only just seen your question, hence the long delay.

Wessex is an ancient Saxon kingdom of England. It was in the south west of the country which is where I live, and is still a used name if not a recognised region. The region is where my roots are, and where I still live, hence my affinity with the area.

Wessex was the greatest ancient kingdom, and fought hard against the invading Danes, especially its greatest king, Alfred the Great (whom you may know of from the myth of King Alfred who burnt the cakes).

There is a good explanation in wikipedia http://en.wikipedia.org/wiki/Wessex
 
Upvote 0
Only just seen your question, hence the long delay.

Wessex is an ancient Saxon kingdom of England. It was in the south west of the country which is where I live, and is still a used name if not a recognised region. The region is where my roots are, and where I still live, hence my affinity with the area.

Wessex was the greatest ancient kingdom, and fought hard against the invading Danes, especially its greatest king, Alfred the Great (whom you may know of from the myth of King Alfred who burnt the cakes).

There is a good explanation in wikipedia http://en.wikipedia.org/wiki/Wessex

Thanks for the info. Having majored in History, it's refreshing to learn a little about Geography and History as well as Excel and macros.
 
Upvote 0

Forum statistics

Threads
1,214,782
Messages
6,121,532
Members
449,037
Latest member
tmmotairi

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top