+ Reply to Thread
Results 1 to 6 of 6

Using Workbook Index as Condition to Auto Close

Hybrid View

lsteinbach Using Workbook Index as... 05-29-2013, 09:43 PM
HaHoBe Re: Using Workbook Index as... 05-29-2013, 11:54 PM
lsteinbach Re: Using Workbook Index as... 05-30-2013, 12:41 AM
HaHoBe Re: Using Workbook Index as... 05-30-2013, 12:48 AM
AlphaFrog Re: Using Workbook Index as... 05-30-2013, 12:56 AM
lsteinbach Re: Using Workbook Index as... 05-30-2013, 08:11 AM
  1. #1
    Forum Contributor
    Join Date
    09-13-2012
    Location
    Midwest
    MS-Off Ver
    Excel 365
    Posts
    124

    Using Workbook Index as Condition to Auto Close

    I have 2 kinds of workbooks that must be opened in order. However, I cannot specify the workbooks in the code because they will always be different files (Master file opened first followed by subsidiary file) so I am using the workbook index since there will only be 2 files opened at a time and they must be opened in a certain order. I dont know why the following code isnt working; I've tried activeworkbook with no success as well.
    Private Sub Workbook_Open()
    If ThisWorkbook <> Workbooks(1) Then
        MsgBox ("PLEASE CLOSE ALL OTHER EXCEL FILES FIRST.")
        ThisWorkbook.Close
    End If
    End Sub
    Then I would apply similar code to the 2nd workbook except with Workbooks(2) and a different msgbox.
    Any help is MUCH appreciated.
    Thank you.

    Luke

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

    Re: Using Workbook Index as Condition to Auto Close

    Hi, Luke,

    where did you place the code? If it´s your personal workbook you will have 3 workbooks open. I would suggest you work with objects on opening the workbooks instead of their index (maybe using GetOpenFilename for the path and name of Workbook).

    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

  3. #3
    Forum Contributor
    Join Date
    09-13-2012
    Location
    Midwest
    MS-Off Ver
    Excel 365
    Posts
    124

    Re: Using Workbook Index as Condition to Auto Close

    The code is placed in THISWORKBOOK. The reason I'm trying to stay away from file names is because there are around 15 different Master files that will be the first workbook open, then around 20 different subsidiary files that could be the second workbook open. So I'd like to stay away from naming all these different files in the coding. When it comes to coding, my thought process can be somewhat linear, so if I am not understanding what you suggested, can you please provide a brief example?
    Also, the computers running these two workbooks will be dedicated kiosks so there is no interference with any other workbooks.
    Danke für jede Hilfe (google translate).

    Luke

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

    Re: Using Workbook Index as Condition to Auto Close

    Hi, Luke,

    maybe try this change which will count the number of workbooks in the instance:
    Private Sub Workbook_Open()
    If Workbooks.Count > 1 Then
        MsgBox ("PLEASE CLOSE ALL OTHER EXCEL FILES FIRST.")
        ThisWorkbook.Close
    End If
    End Sub
    Maybe you should alter the code to open the second workbook from this procedure.
    Private Sub Workbook_Open()
    Dim FileToOpen As Variant
    If Workbooks.Count > 1 Then
        MsgBox ("PLEASE CLOSE ALL OTHER EXCEL FILES FIRST.")
        ThisWorkbook.Close
    Else
      FileToOpen = Application.GetOpenFilename("Excel Files (*.xls*), *.xls*")
      If FileToOpen <> False Then
        Workbooks.Open FileToOpen
      End If
    End If
    End Sub
    Ciao,
    Holger

  5. #5
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,653

    Re: Using Workbook Index as Condition to Auto Close

    Quote Originally Posted by lsteinbach View Post
    The code is placed in THISWORKBOOK. The reason I'm trying to stay away from file names is because there are around 15 different Master files that will be the first workbook open, then around 20 different subsidiary files that could be the second workbook open. So I'd like to stay away from naming all these different files in the coding. When it comes to coding, my thought process can be somewhat linear, so if I am not understanding what you suggested, can you please provide a brief example?
    Also, the computers running these two workbooks will be dedicated kiosks so there is no interference with any other workbooks.
    Danke für jede Hilfe (google translate).

    Luke
    If the "master" workbooks only have the macro then try something like this...

    Sub MyMacro()
        
        Dim wbSubsidiary As Workbook
        
        If Workbooks(1) Is ThisWorkbook Then
            Set wbSubsidiary = Workbooks(2)
        Else
            Set wbSubsidiary = Workbooks(1)
        End If
        
        MsgBox "Master workbook = " & ThisWorkbook.Name & vbLf & _
               "Subsidiary workbook = " & wbSubsidiary.Name
            
    End Sub
    Alternatively, you could loop through each opened workbook and look for a difference (cell.value, sheet name) that distinguishes a Master from a Subsidiary.
    Last edited by AlphaFrog; 05-30-2013 at 01:01 AM.

  6. #6
    Forum Contributor
    Join Date
    09-13-2012
    Location
    Midwest
    MS-Off Ver
    Excel 365
    Posts
    124

    Re: Using Workbook Index as Condition to Auto Close

    Thanks for the ideas! The workbook.count is exactly what I was looking for. Ill be able to alter what you guys provided to suit me as needed. Thank you both!

    Luke

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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