+ Reply to Thread
Results 1 to 4 of 4

Macro to open/close a file only if it isn't already opened/closed

  1. #1
    Registered User
    Join Date
    05-19-2010
    Location
    Pennsylvania
    MS-Off Ver
    Excel 2003
    Posts
    16

    Macro to open/close a file only if it isn't already opened/closed

    I have a data validation set up in a set of files (A.xls, B.xls, C.xls) w/ the valid data lists (vdl.xls) in a separate workbook. I have macros in A, B, and C that run upon opening and closing that open and close vdl.xls. If a user opens both A & B, a window pops up with an error that vdl.xls is already open, and when they are both closed, a run-time error occurs. (Because closing A already closed vdl.xls, and upon closing B, the macro tries to close vdl.xls, but it is already closed.)

    Is there code I can add to A, B & C that will only open/close vdl.xls if it isn't already opened/closed?

    The code currently looks like this:

    Private Sub Workbook_BeforeClose(Cancel As Boolean)

    Workbooks("vdl.xls").Close SaveChanges:=False

    End Sub

    Private Sub Workbook_Open()

    Application.ScreenUpdating = False

    ChDir "T:\\HQ"
    Workbooks.Open Filename:="T:\\HQ\vdl.xls"
    ActiveWindow.Visible = False

    Application.ScreenUpdating = True

    End Sub

  2. #2
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Macro to open/close a file only if it isn't already opened/closed

    Please Login or Register  to view this content.

  3. #3
    Forum Contributor pierre08's Avatar
    Join Date
    02-16-2010
    Location
    Paris
    MS-Off Ver
    Excel 2003
    Posts
    166

    Re: Macro to open/close a file only if it isn't already opened/closed

    Or you can use this function:

    Please Login or Register  to view this content.
    Hope this help

  4. #4
    Registered User
    Join Date
    05-19-2010
    Location
    Pennsylvania
    MS-Off Ver
    Excel 2003
    Posts
    16

    Re: Macro to open/close a file only if it isn't already opened/closed

    Thanks to you both! I used the shorter version and got a compile error. I just had to put in the End If statement at the end.

    I appreciate the assistance!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1