title: Excel VBA - Closing a specific File Explorer window out of multiple open File Explorer windows
body: <p>Cell A3 contains folder path. Cells below contain file names with extensions. Upon selecting a cell below, my Excel macro opens that file's location in File Explorer and out of multiple files in that folder selects this particular one, which can be seen in Preview. When next cell containing another file name is selected on the spreadsheet, another File Explorer window opens, even though it's the same path from A3.  Looking for a line of code to add which will first close the first File Explorer window, before opening a new one. The code needs to be closing that specific File Explorer window from cell A3, out of multiple open File Explorer windows. Code I have so far</p>
  
  <p>UPDATE: Running below codes, but it does not close the existing opened folder, just opens yet another:</p>
  
  <pre><code>If Target.Column = 1 And Target.Row &gt; 5 Then
  
  Call CloseWindow
  
  Shell "C:\Windows\explorer.exe /select," &amp; Range("A3") &amp; ActiveCell(1, 1).Value, vbNormalFocus 'this works, but opens NEW folder every time
  </code></pre>
  
  <p>and in separate Module:</p>
  
  <pre><code>'BELOW GOES WITH Public Sub CloseWindow() FROM: https://stackoverflow.com/questions/49649663/close-folder-opened-through-explorer-exe
  Option Explicit
  
  ''for 64-bit Excel use
  'Private Declare PtrSafe Function SendMessage Lib "user32" Alias "SendMessageA" _
  '    (ByVal hWnd As LongPtr, ByVal wMsg As Long, ByVal wParam As LongPtr, lParam As Long) As LongPtr
  ''for 32-bit Excel use
  'Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
  '    (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Long) As Long
  
  'To make it compatible with both 64 and 32 bit Excel you can use
  #If VBA7 Then
      Private Declare PtrSafe Function SendMessage Lib "user32" Alias "SendMessageA" _
          (ByVal hWnd As LongPtr, ByVal wMsg As Long, ByVal wParam As LongPtr, lParam As Long) As LongPtr
  #Else
      Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
          (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Long) As Long
  #End If
  'Note that one of these will be marked in red as compile error but the code will still run.
  
  
  Const WM_SYSCOMMAND = &amp;H112
  Const SC_CLOSE = &amp;HF060
  
  Public Sub CloseWindow()
      Dim sh As Object
      Set sh = CreateObject("shell.application")
  
      Dim w As Variant
      For Each w In sh.Windows
          'print all locations in the intermediate window
          Debug.Print w.LocationURL
  
          ' select correct shell window by LocationURL
  '        If w.LocationURL = "file://sharepoint.com@SSL/DavWWWRoot/sites/folder" Then
          'If w.LocationURL = "Range("M1").value" Then
          If w.LocationURL = "file://K:/ppp/xx/yy/1 - zzz" Then
              SendMessage w.hWnd, WM_SYSCOMMAND, SC_CLOSE, 0
          End If
      Next w
  End Sub
  </code></pre>
  
  <p>UPDATE 2:<br>
  I am now thinking however, that probably the best solution would actually be not to close the file explorer and then open it, but rather for the code to identify that there is already an open file explorer window with path from cell A3 and neither close it nor open a new one, but rather just select the new file corresponding to the new cell being clicked on in already opened file explorer window with path from cell A3. Can anybody think of a way to do that?</p>