Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select multiple files in Windows Explorer from selected cells in Excel using VBA?

Tags:

excel

vba

I have several folders containing over 1000 subfolders each. I have to move some of them (about half) to other locations depending on the progress made within eachsubfolder. The progress is notated in a spreadsheet, which also provides its path. I have the following code:

    Sub open_explorer()
    Shell "C:\Windows\explorer.exe /select, K:\user\folder\A\" & ActiveCell.Value, vbMaximizedFocus
    End Sub

So, this code will open a window explorer with a file selected (such file is the one that follows the path + ActiveCell value. Is there a way to select multiple files at once? Lets say I want to select 200 cells, so Window Explorer will open with the 200 files selected?

Thank you for the help!

like image 759
Alo Avatar asked Sep 05 '14 20:09

Alo


People also ask

How do I select multiple items in Windows Explorer?

Click on one of the files or folders you want to select. Hold down the control key (Ctrl). Click on files or folders that you want to select while holding the control key. Continue to Hold down the control key until you select all the files you want.

How do I select multiple files in Windows 10 Explorer?

Selecting multiple files by holding down the Ctrl or Shift key is something that every Windows user should be able to do, as it's fast and convenient.

How do you select multiple files in Excel?

By keyboard: First, press F6 to activate the sheet tabs. Next, use the left or right arrow keys to select the sheet you want, then you can use Ctrl+Space to select that sheet. Repeat the arrow and Ctrl+Space steps to select additional sheets.


1 Answers

Unfortunately /select option will only let you select single file. There is no other option which will let you select multiple files. You may confirm that by checking this MS KB Article

Having said that, is it possible to achieve that in VBA since the API SHOpenFolderAndSelectItems is not available? The answer is

YES

Follow these steps.

  1. Open a module and add a reference to Microsoft Shell Controls and Automation and Microsoft Internet Controls as shown below

    enter image description here

  2. Next for testing purpose, we will take the folder C:\Users\Siddharth Rout\Desktop\Test1 which has 5 csv files numbered from 1 to 5 as shown below.

    enter image description here

  3. Now paste the below code in a module and run the procedure Sub Sample()

Code:

Option Explicit

Sub Sample()
    SelectMultipleFiles "C:\Users\Siddharth Rout\Desktop\Test1"
End Sub

Sub SelectMultipleFiles(sFolder As String)
    Dim wb As WebBrowser
    Dim objExp As Shell32.Shell
    
    Set objExp = New Shell32.Shell
    
    objExp.Open sFolder
    
    '~~> Find our explorer window
    Do While wb Is Nothing: Set wb = GetExplorer(sFolder): Loop
    
    '~~> We are going to select files 1,3 and 5.csv
    '~~> The 5& is used so that any previous selections are cleared off
    Call wb.document.SelectItem(sFolder & "\1.csv", 5&)
    Call wb.document.SelectItem(sFolder & "\3.csv", 1&)
    Call wb.document.SelectItem(sFolder & "\5.csv", 1&)
End Sub

'~~> Function to find the releavnt explorer window
Function GetExplorer(sFolder As String) As WebBrowser
    Dim objExp As New Shell32.Shell
    Dim wb1 As WebBrowser

    For Each wb1 In objExp.Windows
        If wb1.Name = "Windows Explorer" And _
        LCase(wb1.document.Folder.Self.Path) = LCase(sFolder) Then
            Set GetExplorer = wb1
        End If
    Next
End Function

Output:

enter image description here

NOTE: As mentioned by @ChrisB, in Windows 10, the WebBrowser.Name property returns File Explorer instead of Windows Explorer so to make it compatible for both versions you can use

If wb1.Name = "Windows Explorer" or wb1.Name = "File Explorer"....

Alternatively you can use WMI to find the Windows version and then choose between Windows/File Explorer

like image 52
Siddharth Rout Avatar answered Sep 21 '22 06:09

Siddharth Rout