Match Folder Name with cell value

babar2019

Board Regular
Joined
Jun 21, 2019
Messages
93
Hello,

I'm trying to open the folder which matches with the cell 'B4' value.

Example if B4 is the date value 08/08/2019, I first would like to format 'B4' to "MM YYYY" format and compare that with the folder names and open the right match. In this case it should open 08 2019 folder.

Please advise
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
What is the folder path which contains the example '08 2019' subfolder?

This macro uses Application.DefaultFilePath as the folder path, which is usually C:\Users\your username\Documents, so it opens C:\Users\your username\Documents\08 2019, if it exists.

Code:
Public Sub Open_Folder()

    Dim path As String
    
    path = Application.DefaultFilePath & "\" & Format(Range("B4").Value, "MM YYYY")
    
    If Dir(path, vbDirectory) <> vbNullString Then
        Shell "explorer.exe """ & path & """", vbNormalFocus
    Else
        MsgBox "The folder " & path & vbCrLf & _
               "doesn't exist"
    End If
    
End Sub
 
Upvote 0
Hi John,

thanks.. the folder path is P:lending\nonpublic\IT\Applications\Test

The excel would be in Test folder
All the folders would be under Applications folders such as 01 2019, 02 2019, 03 2019 etc..

I want the B4 cell range to match with one of these folders and open the folder.

I don’t want to hard code the folder location because it is used in different places. The Vba has to go back one step from where the excel was launched and find the matched folder to open..
 
Upvote 0
Try this macro:
Code:
Public Sub Open_Folder2()

    Dim folderPath As String
        
    folderPath = ThisWorkbook.Path & "\..\" & Format(ActiveSheet.Range("B4").Value, "MM YYYY")
    
    If Dir(folderPath, vbDirectory) <> vbNullString Then
        Shell "explorer.exe """ & folderPath & """", vbNormalFocus
    Else
        MsgBox "The folder " & vbCrLf & vbCrLf & _
               folderPath & vbCrLf & vbCrLf & _
               "doesn't exist"
    End If
    
End Sub
PS - If you don't like the "\..\" in the warning message which is displayed if the folder path doesn't exist (most users probably don't understand what "\..\" in a folder path means) then I can modify the code to use a FileSystemObject method which resolves the "\..\" to the actual folder name.
 
Upvote 0
Hello, How can you not have the folder window visible?

I'm trying to execute a piece of code which converts the excel to pdf and saves it into this folder after it recognizes the right folder match. The folder open should just happen in the background without user seeing it.

Thanks
 
Upvote 0

Forum statistics

Threads
1,214,385
Messages
6,119,209
Members
448,874
Latest member
b1step2far

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