Map drive to a local folder using vba

joshiroc

New Member
Joined
Dec 6, 2018
Messages
2
Hi all,

I've done a fair amount of searching and trying different methods myself; however, I am unable to map a drive letter to a local folder on my machine.

This thread shows how to map to a network drive, and it works well for actual network locations: https://www.mrexcel.com/forum/excel-questions/284297-map-network-drive-using-excel-macro.html

For mapping to a local folder, I can do this in CMD using subst, so I have tried several permutations of this code:

Code:
Private Sub mapdrive2()
    Dim MyDriveName As String
    Dim MyDrive As Object
    MyDriveName = """C:\Users\me\"""


    Call Shell("cmd.exe Subst N: " & MyDriveName, vbNormalFocus)
    
    '- error check
    If Err.Number <> 0 Then
        MsgBox (" Drive already mapped or not available ")
    Else
        MsgBox ("Mapped OK")
    End If
    Set MyDrive = Nothing


End Sub

This code opens CMD; however the N: drive is not 'mapped' and I can't seem to get it to work.

Any ideas for what to try?

Thanks,

Josh
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Hi Josh
Welcome to the board

Try:

Code:
Private Sub mapdrive2()
    Dim MyDriveName As String
    
    MyDriveName = "C:\Users\me"

    Call Shell("Subst N: " & MyDriveName, vbNormalFocus)
    
    '- error check
    If Err.Number <> 0 Then
        MsgBox (" Drive already mapped or not available ")
    Else
        MsgBox ("Mapped OK")
    End If

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,599
Messages
6,120,449
Members
448,966
Latest member
DannyC96

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