Results 1 to 7 of 7

Thread: How do I check to see if a worksheet is already open in excel?

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2005
    Posts
    14

    How do I check to see if a worksheet is already open in excel?

    I want to activate a specific worksheet at a certain time. However if that worksheet is not already open there will be an error.

    What I want to do is check to see if that worksheet is open and if it isn't then I'll make the program open it.

    If it is already open then i'll simply activate it as planned.

    Thanks

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: How do I check to see if a worksheet is already open in excel?

    This should do it.
    VB Code:
    1. Dim oWB As Excel.Workbook
    2.     Dim i As Integer
    3.     Dim bFound As Boolean
    4.     For i = 1 To Application.Workbooks.Count
    5.         If Application.Workbooks(i).Name = "SomeWorkbookThatIAmLookingFor.xls" Then
    6.             bFound = True
    7.             Exit For
    8.         End If
    9.     Next
    10.     If bFound = True Then
    11.         MsgBox "Open alreaady"
    12.         Set oWB = Application.Workbooks("SomeWorkbookThatIAmLookingFor.xls")
    13.     Else
    14.         MsgBox "Not Open"
    15.         Set oWB = Application.Workbooks.Open("SomeWorkbookThatIAmLookingFor.xls")
    16.     End If
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    New Member
    Join Date
    Jun 2005
    Posts
    14

    Re: How do I check to see if a worksheet is already open in excel?

    Hmm, it doesn't seem to be working either.

    There is something not working correct in this part:

    VB Code:
    1. For i = 2 To Application.Workbooks.Count
    2.            
    3.                 If Application.Workbooks(i).Name = "H:\rrusnak\Equipment Log.xls" Then
    4.                     bFound = True
    5.                     Exit For
    6.                 End If
    7.                 bFound = True
    8.             Next

    It isn't getting the true part even when the workbook is already open. But I don't really understand what that if statement is doing so I'm not sure how to solve it.


    Also, even if I manually set bFound to true, it will go to the message "already open". However after that it just says subscript out of range. Not sure why. What I need to happen in that case is for the other workbook to just be activated.


    For example, when I run this macro I'm in workbook A. It checks to see if workbook B is open. If workbook B is open, then it just needs to activate it. If it isn't open, then it needs to open it and have it active.

    When bFound is false, I get the message "not open" and then it opens workbook B, but then it tries opening it again and causes an error.

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: How do I check to see if a worksheet is already open in excel?

    Step through the code by pressing F8 checking the values in the workbooks collection. It works, just need to determine
    whats going on. Why did you change it to start from item #2?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5

    Thread Starter
    New Member
    Join Date
    Jun 2005
    Posts
    14

    Re: How do I check to see if a worksheet is already open in excel?

    Oh I was just trying something with the number 2 and forgot to change it back.

    Anyway, I found where the first error is happening:

    [Highlight=VB]
    Next
    If bFound = True Then
    MsgBox "Open already"
    Set oWB = Application.Workbooks("H:\rrusnak\Equipment Log.xls")
    Else
    MsgBox "Not Open"
    Set oWB = Application.Workbooks.Open("H:\rrusnak\Equipment Log.xls")
    End If

    The bolded part is where it is happening. Not sure why though.


    It says: Runtime error '9', subscript out of range

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: How do I check to see if a worksheet is already open in excel?

    and then it opens workbook B, but then it tries opening it again and causes an error.
    Is this the same error or new error?

    Why, if its the same error, is it trying to open it twice? If its opened, is it in the same application instance or a separate one? You can
    tell by going to the Window menu and seeing the opened workbooks in that instance. I dont believe it can detect opened workbooks
    in other instances.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  7. #7

    Thread Starter
    New Member
    Join Date
    Jun 2005
    Posts
    14

    Re: How do I check to see if a worksheet is already open in excel?

    Both applications are open in the same instance.

    I don't really understand why its opening it a second time but it does. I think the reason is because it isn't getting a true value even when it is already open so it keeps trying.

    hmm, well I'll post all the code in case you feel like looking at it.

    By the way, what exactly does the Next statement do? I've never used that before.


    VB Code:
    1. Option Explicit
    2.  
    3.  
    4. Sub Find2()
    5. '
    6. ' Find2 Macro
    7. ' Macro recorded 06/23/2005 by
    8. '
    9.  
    10.        
    11.     Range("A5").Select
    12.    
    13.    
    14.     Cells.Find(What:="F00", after:=activeCell, LookIn:=xlValues, Lookat:= _
    15.     xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:= _
    16.     False, SearchFormat:=False).activate
    17.    
    18.     Dim Issue As String
    19.     Dim Date1 As String
    20.    
    21.     Issue = Range("B3").Value
    22.     Date1 = Range("B2").Value
    23.  
    24.     Do Until activeCell.Value = "F00----"
    25.    
    26.         If activeCell.Value = "F00" Then
    27.             Cells.Find(What:="F00", after:=activeCell, LookIn:=xlValues, Lookat:= _
    28.             xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:= _
    29.             False, SearchFormat:=False).activate
    30.        
    31.         Else
    32.             Dim carry As String
    33.             carry = activeCell.Value
    34.        
    35.             Dim oWB As Excel.Workbook
    36.             Dim i As Integer
    37.             Dim bFound As Boolean
    38.            
    39.             For i = 1 To Application.Workbooks.Count
    40.            
    41.                 If Application.Workbooks(i).Name = "H:\rrusnak\Equipment Log.xls" Then
    42.                     bFound = True
    43.                     Exit For
    44.                 End If
    45.                 bFound = True
    46.             Next
    47.             If bFound = True Then
    48.                 MsgBox "Open already"
    49.                 Set oWB = Application.Workbooks("H:\rrusnak\Equipment Log.xls")
    50.             Else
    51.                 MsgBox "Not Open"
    52.                 Set oWB = Application.Workbooks.Open("H:\rrusnak\Equipment Log.xls")
    53.             End If
    54.            
    55.             Dim finish As Boolean
    56.             Dim index As Integer
    57.             index = 1
    58.             finish = False
    59.            
    60.             Do Until finish = True
    61.            
    62.                 If index = 12 Then
    63.                
    64.                     Dim Msg, Style, Title, Help, Ctxt, Response, MyString
    65.                     Msg = "YTG Stock #" + carry + " Not Found"    ' Define message.
    66.                     Style = vbOKOnly + vbExclamation + vbDefaultButton1    ' Define buttons.
    67.                     Title = "Stock Number Not Found"    ' Define title.
    68.                     Help = "DEMO.HLP"    ' Define Help file.
    69.                     Ctxt = 1000    ' Define topic
    70.                     ' context.
    71.                     ' Display message.
    72.                     Response = MsgBox(Msg, Style, Title, Help, Ctxt)
    73.                    
    74.                     If Response = vbYes Then    ' User chose Yes.
    75.                         MyString = "Yes"    ' Perform some action.
    76.                     End If
    77.  
    78.                     finish = True
    79.                 Else
    80.                
    81.                     Worksheets(index).activate
    82.                
    83.                     Range("B4").Select
    84.                
    85.                     Do Until activeCell.Value = "" Or finish = True
    86.                    
    87.                         If activeCell.Value = carry Then
    88.                             activeCell([1], [2]).Select
    89.                             activeCell = Issue
    90.                             activeCell([1], [2]).Select
    91.                             activeCell = Date1
    92.                             finish = True
    93.                         Else
    94.                             activeCell.Offset([1], [0]).activate
    95.                         End If
    96.        
    97.                     Loop
    98.                     index = index + 1
    99.                 End If
    100.             Loop
    101.             Windows("Material Reconciliation Slips.xls").activate
    102.            
    103.             Cells.Find(What:="F00", after:=activeCell, LookIn:=xlValues, Lookat:= _
    104.             xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:= _
    105.             False, SearchFormat:=False).activate
    106.        
    107.         End If
    108.        
    109.        
    110.    
    111.     Loop
    112.    
    113. Windows("Equipment Log.xls").activate
    114. Worksheets(1).activate
    115. Windows("Material Reconciliation Slips.xls").activate
    116.  
    117. End Sub

Posting Permissions

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



Click Here to Expand Forum to Full Width