Will this work in cells that point to other workbooks? Where the cell value is =G:\XXX\XXXX.xls
will it change it to =\\server\XXX\XXXX.xls ?
The code won't change cells, but it could be called from a procedure that converts references to external workbooks to use UNC paths instead of mapped drive letters.
References to an external workbook which is closed would look something like this:
='G:\Folder\SubFolder\[MyFile.xls]Sheet1'!$A$2
and could be converted to
='\\Server\Folder\SubFolder\[MyFile.xls]Sheet1'!$A$2
To convert references to a specific drive found in a range you could use....
Code:
Sub ReplaceWithUNC()
Const sDriveLetter As String = "G:"
Dim sUNC As String
sUNC = GETNETWORKPATH(sDriveLetter)
If sUNC = "" Then
MsgBox "Drive letter " & sDriveLetter & " not mapped."
Else
With ActiveSheet.Range("A1:A100") ' or Selection
.Replace What:=sDriveLetter & "\", Replacement:=sUNC & "\", _
LookAt:=xlPart, MatchCase:=False
End With
End If
End Sub
Two cautions in using that code:
1. It will convert any matching text "G:\" to the UNC, even if it isn't part of a formula reference to that drive.
2. It won't work on references to closed workbooks, since the paths aren't shown the formula is simply =[MyFile.xls]Sheet1'!$A$2
If you want to convert references to a specific drive throughout the entire workbook, another approach would be to remap the workbook links instead of the separate formulas.
Code:
Sub ConvertLinksToUNC()
Dim sUNC As String
Dim vLinks As Variant
Dim i As Long
Const sDriveLetter As String = "G:"
sUNC = GETNETWORKPATH(sDriveLetter)
If sUNC = "" Then
MsgBox "Drive letter " & sDriveLetter & " not mapped."
Exit Sub
End If
With ActiveWorkbook
vLinks = .LinkSources(xlExcelLinks)
If Not IsEmpty(vLinks) Then
For i = 1 To UBound(vLinks)
.ChangeLink Name:=vLinks(i), _
NewName:=Replace(vLinks(i), Find:=sDriveLetter & "\", _
Replace:=sUNC & "\", Compare:=vbTextCompare)
Next i
End If
End With
End Sub
This has the benefit of only modifying the true links and won't modify matching text isn't a link.
Though this will remap the links for external workbooks that are currently open, I'd recommend closing the linked workbook(s) first.