+ Reply to Thread
Results 1 to 7 of 7

Check Open Workbooks Before Closing

Hybrid View

  1. #1
    Registered User
    Join Date
    08-10-2012
    Location
    US
    MS-Off Ver
    Excel 2003
    Posts
    41

    Check Open Workbooks Before Closing

    Hello,

    I have a workbook ("WBK1") containing data that other workbooks source information from. My thought is to place a certain value ("XX") in cell "A1" of the associated workbooks and check all open workbooks for "XX" in cell "A1". If any of the open workbooks contains "XX" in "A1" then a userform will display, otherwise "WBK1" will just close.

    Any code suggestions?

  2. #2
    Valued Forum Contributor
    Join Date
    12-02-2012
    Location
    Melbourne, VIC
    MS-Off Ver
    Excel 2016
    Posts
    750

    Lightbulb Re: Check Open Workbooks Before Closing

    try playing with this:
    Sub Workbook_Check()
    Dim Wb as Workbook, bClose as boolean
    bClose = True
    For Each Wb In Application.Workbooks
         With Wb
              If .Worksheets("Sheet1").Range("A1") = "XX" Then 
                   Form1.Show
                   bClose = False
                   Exit For
                   End If
              End With               
         Next Wb
    If bClose = True Then Application.Workbooks("WBK1").Close
    End Sub

  3. #3
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Check Open Workbooks Before Closing

    Hi, jewelsharma,

    from what I remember you have got to use the extension for the workbook as well not the name solely.

    If WBK1 is the workbook with code the line could read
    If bClose = True Then ThisWorkbook.Close True 'save with changes
    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  4. #4
    Valued Forum Contributor
    Join Date
    12-02-2012
    Location
    Melbourne, VIC
    MS-Off Ver
    Excel 2016
    Posts
    750

    Thumbs up Re: Check Open Workbooks Before Closing

    Hi Guru Holger,
    I seem to be misinformed. Somehow, I had an understanding that an already opened workbook could be referred to simply by its extention-less name. Nevertheless, per the details provided by OP, it seems the code would rest in the named workbook; and
    ThisWorkbook.Close True
    would be perfect here.
    Many thanks for your review. Cheers!

  5. #5
    Registered User
    Join Date
    08-10-2012
    Location
    US
    MS-Off Ver
    Excel 2003
    Posts
    41

    Re: Check Open Workbooks Before Closing

    Thanks for the replies however I'm having problems with

    If .Worksheets("Sheet1").Range("A1") = "XX" Then

    The debugger runs and highlights this line. I changed "Sheet1" to the subject sheet however no luck.

    If I remove the "." before ".Worksheets" and change "Sheet1" to the name of a sheet in the workbook I want to close it works however that isn't the workbook I want to search. If I change "Sheet1" to the name of a sheet in another workbook the debugger runs.

    Anymore ideas?

  6. #6
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Check Open Workbooks Before Closing

    Hi, ezrizer,

    Sub Workbook_Check()
    Dim Wb As Workbook
    Dim blnCheck As Boolean
    
    Const cstrVALUE As String = "XX"
    
    For Each Wb In Application.Workbooks
      If Wb.Worksheets("Sheet1").Range("A1") = cstrVALUE Then
        blnCheck = True
      End If
    Next Wb
    
    If blnCheck = True Then
      UF1.Show
    Else
      ThisWorkbook.Close True
    End If
    End Sub
    Ciao,
    Holger

  7. #7
    Registered User
    Join Date
    08-10-2012
    Location
    US
    MS-Off Ver
    Excel 2003
    Posts
    41

    Re: Check Open Workbooks Before Closing

    Holger,

    Thanks for the reply. Below is what I was able to make work

    [code]
    Dim Wb As Workbook
    Dim Ws As Worksheet
    Dim blnCheck As Boolean
    For Each Wb In Application.Workbooks
    For Each Ws In Wb.Worksheets
    If Ws.Range("A1") = "XServiceToolsX" Then
    blnCheck = True
    End If
    Next Ws
    Next Wb
    If blnCheck = True Then
    CloseProject.Show
    Else
    ThisWorkbook.Close True
    End If
    [code]

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Macro to autoclose hidden workbooks is closing all active workbooks
    By tlchavez in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-23-2013, 06:38 PM
  2. [SOLVED] Closing VBA - condition: number of open workbooks
    By MitchelDZ in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-12-2012, 07:24 AM
  3. Closing workbooks
    By deeppurple247 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-04-2007, 05:01 PM
  4. Closing Workbooks
    By bodhisatvaofboogie in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-20-2006, 09:00 AM
  5. [SOLVED] Check if several workbooks are open
    By Ricardo Silva in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-23-2006, 05:55 PM

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