How to open a folder with VBA
Solved
supercopain
-
debutant_en_macro -
debutant_en_macro -
Hello,
I'm looking for a macro that opens a folder whose name is in an Excel list.
For example, in a summary list I have E1234.
In a folder there are several subfolders, only one is named E1234, etc.
When the cell in my list contains E1234 I want to click a button to open the folder with that same name.
Thank you very much.
<config>Windows XP / Office 2003</config>
I'm looking for a macro that opens a folder whose name is in an Excel list.
For example, in a summary list I have E1234.
In a folder there are several subfolders, only one is named E1234, etc.
When the cell in my list contains E1234 I want to click a button to open the folder with that same name.
Thank you very much.
<config>Windows XP / Office 2003</config>
14 answers
-
I added a message
Remember to mark as resolved if it's okay!
the message will appear at the bottom left of the sheet,
every 20 folders.
Where you see 'Ready'Option Explicit Public V As String Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" _ (ByVal hwnd As Long, ByVal lpOperation As String, _ ByVal lpFile As String, ByVal lpParameters As String, _ ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long Sub OuvreDossier() Dim Target As Range V = "" Dim chainePath As String Set Target = ActiveCell chainePath = "G:\Docu\R&D\Rapports d'essais" 'chainePath = "C:\Data\2009" If Not Intersect(Target, Range("A2:A20")) Is Nothing Then ' la plage qui contient les essais Application.ScreenUpdating = False V = Left(Replace(ActiveCell.Value, "E", "E ", 1), 6) Call AfficherListeDossiers(chainePath, CreateObject("scripting.filesystemobject"), V) If V <> "" Then ShellExecute 0, "explore", V, "", "", 10 End If End If End Sub Public Sub AfficherListeDossiers(ByVal specdossier As String, ByRef Fso, ByVal leChemin) ' Dim j As Integer Dim msgStatusBarre As String msgStatusBarre = Application.DisplayStatusBar ' sauvegarde Dim dossier, fd, sDossier ' variable de type variant ' Set Fso = CreateObject("Scripting.FileSystemObject") ' créé un objet fileSystem Set dossier = Fso.GetFolder(specdossier) ' dossier : héritage de fso Set sDossier = dossier.SubFolders 'sDossier : héritage de dossier j = 0 For Each fd In sDossier ' Scanne la collection sDossier j = j + 1 If (j Mod 20) = 0 Then ' mise à jour du message tous les 20 dossiers Application.StatusBar = j & " traités sur : " & sDossier.Count End If If InStr(1, fd.Name, V) > 0 Then V = fd Exit For End If Next Set sDossier = Nothing Set dossier = Nothing Application.StatusBar = False Application.DisplayStatusBar = msgStatusBarre End Sub