Open windows explorer to a folder

digita

New Member
Joined
Jun 5, 2009
Messages
8
Hi,

I have this hard wired code to open a folder called Comm. It works perfectly.

Code:
Shell "explorer.exe ""C:\Users\Pas\Comm\", 1

I have this full folder path recorded in cell A1. Just to make it a bit more flexible, I loaded this value to a string variable and execute the same script to open this same folder

Code:
Fldr = Range("A1").value
Shell "explorer.exe " & Fldr, 1

Windows explorer opens up in an incorrect folder. Just wonder why. Hope someone would know the answer.

Thanks.

kp
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
When I do it using the same code, explorer opens to the correct folder.

What is the value of your cell A1?
What is the "incorrect" folder that explorer is opening up to instead of the folder specified in A1?
What if you change your code to this?

Code:
Fldr = Range("A1").value
Shell "explorer.exe " & Fldr & " , 1"
 
Upvote 0
When I do it using the same code, explorer opens to the correct folder.

What is the value of your cell A1?
What is the "incorrect" folder that explorer is opening up to instead of the folder specified in A1?
What if you change your code to this?

Code:
Fldr = Range("A1").value
Shell "explorer.exe " & Fldr & " , 1"

Hi Rlv01,

Thanks for responding. The value I have at cell A1 is C:\Users\Pas\Comm\. When I run the above script, Windows Explorer opens up my documents folder - (C:\Users\Pas\Documents) rather the desired folder. I can't work out why. The line below works flawlessly in my script.

Shell "explorer.exe ""C:\Users\Pas\Comm", 1
 
Last edited:
Upvote 0
Ok, let's take it one step at a time. First, let's remove Cell A1 from the equation and hard code the folder location into the subroutine for test purposes. We can also test to see if the desired folder exists. Run this and see if explorer opens to the correct folder:

Code:
Sub Test()
    Dim CmdLine As String, Fldr As String
    Dim FSO As Object

    Set FSO = CreateObject("Scripting.FileSystemObject")
    
    Fldr = "C:\Users\Pas\Comm\"

    If FSO.FolderExists(Fldr) Then
        CmdLine = "explorer.exe " & Fldr & ", 1"
        Debug.Print CmdLine
        Shell CmdLine
    Else
        MsgBox "Folder '" & Fldr & "' does not exist", vbInformation
    End If
End Sub

If it does, then change Fldr to read the value in cell A1 and repeat the test

Code:
Sub Test()
    Dim CmdLine As String, Fldr As String
    Dim FSO As Object

    Set FSO = CreateObject("Scripting.FileSystemObject")
    
    Fldr = ActiveSheet.Range("A1").Value
    
    If FSO.FolderExists(Fldr) Then
        CmdLine = "explorer.exe " & Fldr & ", 1"
        Debug.Print CmdLine
        Shell CmdLine
    Else
        MsgBox "Folder '" & Fldr & "' does not exist", vbInformation
    End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,170
Members
453,021
Latest member
Justyna P

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