VBA - How to open folder without knowing the full name

goncaloColt

New Member
Joined
May 24, 2022
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hello!

I'm trying to open a folder where I don't know the full path.
For example, the parent folder dir is "D:\Documents" and the folder I want to open is called "22.111 - PROJECT_NAME", where I know the code, but don't know the name. I've tried with "*", but no luck.

VBA Code:
Sub OpenFolder()

On Error GoTo Err_cmdExplore_Click

Dim Code As String

Code = Range("A1").Value
GoToFolder = "C:\Windows\explorer.exe D:\Documents\" & Code & "*"

Call Shell(GoToFolder, 1)

Exit_cmdExplore_Click:
Exit Sub

Err_cmdExplore_Click:
MsgBox ("Pasta não encontrada")
Resume Exit_cmdExplore_Click

End Sub

Any ideias?
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Try this:
VBA Code:
Public Sub Find_and_Open_Folder()

    Dim Code As String
    Dim targetFolder As String
    
    Code = Range("A1").Value
    
    targetFolder = Dir("D:\Documents\" & Code & "*", vbDirectory)
    If targetFolder <> vbNullString Then
        Shell "explorer.exe """ & "D:\Documents\" & targetFolder & """", vbNormalFocus
    Else
        MsgBox "Folder matching D:\Documents\" & Code & "* not found"
    End If
    
End Sub
 
Upvote 0
Solution
Try this:
VBA Code:
Public Sub Find_and_Open_Folder()

    Dim Code As String
    Dim targetFolder As String
   
    Code = Range("A1").Value
   
    targetFolder = Dir("D:\Documents\" & Code & "*", vbDirectory)
    If targetFolder <> vbNullString Then
        Shell "explorer.exe """ & "D:\Documents\" & targetFolder & """", vbNormalFocus
    Else
        MsgBox "Folder matching D:\Documents\" & Code & "* not found"
    End If
   
End Sub

Worked like charm! Thank you!
 
Upvote 0

Forum statistics

Threads
1,223,897
Messages
6,175,271
Members
452,628
Latest member
dd2

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top