Unable To Open Explorer To The Specified Folder

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
4,612
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Considering ...

txt_model = "Skittles"
I am trying to create the folder "Skittles" in the specified path using the code below.

VBA Code:
Sub open_folder(txt_model As String)
    Dim modelini As String
    Dim ffound As String
    
    If Right(txt_model, 1) = "." Then txt_model = Left(txt_model, (Len(txt_model) - 1))
    modelini = Left(txt_model, 1)
    fldpath = "M:\IFM\" & modelini & "\" & txt_model & "\"
    ffound = Dir(fldpath)
    If ffound = "" Then 'folder not found. Opt to create one.
        MkDir fldpath
    End If
    Shell "explorer.exe /root," & fdlpath, vbNormalFocus
End Sub

In my testing, "Skittles" does not exist, so the folder is created in the appropriate location.
Once this is done, I want file explorer to open up the newly created folder. But the code here only opens up file explorer to it's most highest level, not to the individual folder level.
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
If I understand your intent correctly, patch up your macro as below:
VBA Code:
'...
If ffound = "" Then                           'folder not found. Opt to create one.
    MkDir "M:\IFM\" & modelini                '<-- added
    MkDir fldpath
End If
Shell "explorer.exe /root," & fldpath, vbNormalFocus '<-- fixed typo (was fdlpath)
'...
 
Upvote 0
Just my opinion but when I read "if you don't use Option Explicit" you get the grief you deserve.
Your folder path variable is spelt differently in different code lines.

EDIT - I think it was actually If you don't use Option Explicit you deserve the grief you get.
 
Upvote 0
Ehm, this would be even better:
VBA Code:
'...
If ffound = "" Then                           'folder not found. Opt to create one.
    On Error Resume Next                      '<-- added
    MkDir "M:\IFM\" & modelini                '<-- added
    On Error GoTo 0                           '<-- added
    MkDir fldpath
End If
Shell "explorer.exe /root," & fldpath, vbNormalFocus '<-- fixed typo (was fdlpath)
'...
 
Upvote 0
Solution
Thank you Rollis.
Micron, the one module I hadn't used Option Explicit. Is there a way to automatically have that injected in code when creating a new module?
 
Upvote 0
You have to manually add it to existing modules. Choosing the option won't insert it in those modules.
 
Upvote 0

Forum statistics

Threads
1,224,755
Messages
6,180,758
Members
452,996
Latest member
nelsonsix66

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