Couple of macros below are doing what I need, but create an issue - it closes Internet Explorer window under certain conditions, instead of File Explorer.
Catch is that without "Call CloseWindow(sPrev)" code keeps opening more and more File Explorer windows.
On Excel worksheet in A3 there's path to file. File names from that path are listed in A6 and down.
Code relevant to this question in first macro below starts with line "If Target.Column = 1 And Target.Row > 5 Then"
Code closes existing File Explorer window, then opens a new one, then selects the next file listed in A6 and down in the newly opened File Explorer window.
Looking for modification of this code for selection of the next file to take place without opening new or closing existent File Explorer window, i.e. select new file in the same File Explorer window.
("Call CopyFirstOne" just copies first set of characters from a cell and not relevant to this question)
Catch is that without "Call CloseWindow(sPrev)" code keeps opening more and more File Explorer windows.
On Excel worksheet in A3 there's path to file. File names from that path are listed in A6 and down.
Code relevant to this question in first macro below starts with line "If Target.Column = 1 And Target.Row > 5 Then"
Code closes existing File Explorer window, then opens a new one, then selects the next file listed in A6 and down in the newly opened File Explorer window.
Looking for modification of this code for selection of the next file to take place without opening new or closing existent File Explorer window, i.e. select new file in the same File Explorer window.
("Call CopyFirstOne" just copies first set of characters from a cell and not relevant to this question)
Code:
Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Static sPrev As String
Static iRow As Long
Dim sFolderName As String, sFullPathName As String
If Target.Rows.Count > 1 Then Exit Sub
If Target.Row < 6 Then Exit Sub
If iRow > 0 Then
Rows(iRow).RowHeight = 15
Rows(iRow).Font.Size = 10
Rows(iRow).Cells.Interior.ColorIndex = 0
End If
iRow = Target.Row
Target.RowHeight = 30
Target.EntireRow.Font.Size = 12
Target.EntireRow.Cells.Interior.ColorIndex = 37
If Target.Column = 1 And Target.Row > 5 Then
sFolderName = Range("A3").Text
sFolderName = IIf(Right(sFolderName, 1) = "\", sFolderName, sFolderName & "\")
sFullPathName = sFolderName & ActiveCell(1, 1).Value
Call CloseWindow(sPrev)
If Len(Dir(sFullPathName)) Then
If GetAttr(sFullPathName) = 32 Then
Shell "C:\Windows\explorer.exe /select," & sFullPathName, vbNormalFocus 'somewhere past this line need for Excel wb to take the focus back as an option
sPrev = sFullPathName
ThisWorkbook.Activate 'for This Excel wb to take the focus back to see if this will fit the workflow
End If
End If
End If
ActiveWindow.ScrollRow = Selection.Row
Call CopyFirstOne
End Sub
Private Sub CloseWindow(ByVal FullPathName As String)
Dim sh As Object
Dim w As Object
On Error Resume Next
Set sh = CreateObject("shell.application")
For Each w In sh.Windows
If Not w.Document Is Nothing Then
If Not w.Document.FocusedItem Is Nothing Then
If w.Document.FocusedItem.Path = FullPathName Then
w.Quit
Exit For
End If
End If
End If
Next w
End Sub