Reputation: 174
I've created a portal for myself and coworkers to download workbook templates that reference different network drives in an effort to consolidate.
The portal is hosted in the "Accounting" (Z:/) drive, but one of the workbooks references a spreadsheet in a different drive "BI" (Y:/).
This works splendidly on my machine, but my coworkers have different drive letters (e.g. M:/Accounting, U:/BI).
Is there a way to search a network for a name and return the drive letter?
Here's a rough approximation of my code:
Option Explicit
Sub mySub()
dim WBaPath as String
dim WBbPath as String
WBaPath = "Y:\BI-Accounting\myWorkbook.xlsm"
WBbPath = "Z:\Portal\myOtherWorkbook.xlsm"
dim WBa as Workbook
dim WBb as Workbook
set WBa = Workbooks.open(WBaPath)
set WBb = ThisWorkbook
'Code to do stuff
End Sub
The folders and files will have the same name, but what is the best way to make sure that all users in my department can use these tools without having to reprogram and, preferably, without having to choose at run-time?
Upvotes: 1
Views: 2023
Reputation: 23974
You can use the FileSystemObject's Drives property to achieve this:
Function FindDriveLetter(shareToFind As String) As String
Dim fs As Object
Dim dc As Object
Dim d As Object
Set fs = CreateObject("Scripting.FileSystemObject")
Set dc = fs.Drives
For Each d In dc
If d.drivetype = 3 Then
If UCase(d.sharename) = UCase(shareToFind) Then
FindDriveLetter = d.driveletter
Exit Function
End If
End If
Next
FindDriveLetter = ""
End Function
Sub Test()
MsgBox FindDriveLetter("\\SERVER01\SHAREXYZ\FILES")
' use whatever share name you have mapped to your "Y" or "Z" drive
' instead of "\\SERVER01\SHAREXYZ\FILES"
End Sub
But using a UNC path (e.g. WBaPath = "\\SERVER01\SHAREXYZ\FILES\BI-Accounting\myWorkbook.xlsm"
) is usually easier.
Upvotes: 2