Excel Guides

Finding Other Instances of Excel in a Macro

When you are working with macros in Excel, you may sometimes need to find other instances of Excel. For example, if you have a macro that opens up a new workbook, you may want to find out if there are any other instances of Excel running so that you can close them before running the macro. Here are some ways to do this:

  1. Check the Task Manager
  2. The first way to check if there are any other instances of Excel running is to open the Task Manager. To do this, press Ctrl+Shift+Esc. Then, click on the "Processes" tab. You should see a list of all the processes that are running on your computer. Look through the list and see if there are any other instances of "EXCEL.EXE" running. If there are, then you know that there are other instances of Excel open.

  3. Use the VBA Shell function
  4. Another way to check if there are any other instances of Excel running is to use the VBA Shell function. This function will return the PID (process ID) of any process that is running on your computer. To use it, open the Visual Basic Editor (press Alt+F11) and insert the following code:

    Dim PID As Long 
    PID = Shell("EXCEL.EXE", vbNormalFocus) 
    If PID <> 0 Then 
        MsgBox "Excel is already running!" 
    End If 
    

    This code will check to see if there is an instance of Excel running and, if there is, it will display a message box saying "Excel is already running!". You can then modify this code to take whatever action you want (e.g. close all other instances of Excel).

  5. Use VBA's GetObject function
  6. GetObject( ) is a function in VBA that allows you to get a reference to an object that is already running in another program. For example, you can use it to get a reference to an instance of Excel that is already open. To use it, insert the following code into the Visual Basic Editor:

    Dim objExcel As Object 
    Set objExcel = GetObject(,"Excel.Application") 
    If Not objExcel Is Nothing Then 
        MsgBox "Excel is already running!" 
    
        'Do something here... 
    
        Set objExcel = Nothing 
    
     End If  

    (This code was adapted from this page)


    This code will try to get a reference to an instance of Excel and, if it succeeds, it will display a message box saying "Excel is already running!". You can then modify this code to take whatever action you want (e.g., close all other instances of Excel).


  7. Use VBA's FindWindow function

  8. FindWindow() is a Windows API function that allows you to find a window by its name or class name . For example , you can use it to find an instance of Excel that is already open . To use it , insert the following code into the Visual Basic Editor :

     Dim hWnd As Long hWnd = FindWindow("XLMAIN", 0&) If hWnd <> 0 Then MsgBox "Excel is already running!" 'Do something here... End If 

    < p > This code will try to find an instance of Excel and , if it succeeds , it will display a message box saying "Excel is already running!" You can then modify this code to take whatever action you want ( e . g . , close all other instances of Excel ) .
    < li > Use VBA's EnumWindows function
    < p >< a href =" https :// msdn . microsoft . com / en - us / library / windows / desktop / ms633528 ( v = vs . 85 ) . aspx " > EnumWindows() is another Windows API function that allows you to enumerate all top - level windows on the screen . For example , you could use it to find all instances of Excel that are currently open . To use it , insert the following code into the Visual Basic Editor : < br /> < pre >< code class =" language - vb " > Dim lngHwnd As Long Dim strTitle As String Do lngHwnd = FindWindowEx(0&, lngHwnd&, vbNullString&, vbNullString&) strTitle = String$(255&, Chr$(0)) GetWindowText lngHwnd&, strTitle&, Len(strTitle) strTitle = Left$(strTitle$, InStr(strTitle$, Chr$(0)) - 1) If InStr(1&, strTitle$, "Microsoft Office Excel") > 0 Then MsgBox strTitle$ End If Loop Until lngHwnd <= 0 End Sub
    < p > This code will enumerate all top - level windows on your screen and look for ones with "Microsoft Office Excel" in their title . If it finds one , it will display its title in a message box . You can then modify this code as needed ( e . g . , close all windows with "Microsoft Office Excel" in their title ). Note : This code uses some Windows API functions and will only work on Windows - based systems . It will not work on Macs or Linux machines.< br />

Move beyond 

Excel

Get started with Causal today.
Build models effortlessly, connect them directly to your data, and share them with interactive dashboards and beautiful visuals.