Hi!
I had a set of ~50 project folders (format e.g. 12345_Project_Name) which mere migrated to another server, but in the process were moved into different subfolders and in some instances were renamed, so I cant, for example, simply rename hyperlinks from \\drive_a\ to \\drive_b\.
I've pulled together some code to navigate to the new drive (e.g. L:\Active_Projects), after which it should run through a range of project IDs and, if found, return the network path to the active cell as a hyperlink.
It works (slowly; ~2 mins) on a single cell, but crashes Excel if I try a range of more than about three cells. Not sure if it is the code or the number of subfolders on the new drive.
Grateful for any advice on how to improve or alternative approaches which may work.
Thanks!
I had a set of ~50 project folders (format e.g. 12345_Project_Name) which mere migrated to another server, but in the process were moved into different subfolders and in some instances were renamed, so I cant, for example, simply rename hyperlinks from \\drive_a\ to \\drive_b\.
I've pulled together some code to navigate to the new drive (e.g. L:\Active_Projects), after which it should run through a range of project IDs and, if found, return the network path to the active cell as a hyperlink.
It works (slowly; ~2 mins) on a single cell, but crashes Excel if I try a range of more than about three cells. Not sure if it is the code or the number of subfolders on the new drive.
Grateful for any advice on how to improve or alternative approaches which may work.
Thanks!
VBA Code:
Sub aa_Test()
Dim myRoot As String
myRoot = BrowseFolder
Application.ScreenUpdating = False
Call Recurse(myRoot)
Application.ScreenUpdating = True
MsgBox "Done!"
End Sub
VBA Code:
Function Recurse(sPath As String) As String
Dim FSO As New FileSystemObject
Dim myFolder As folder
Dim mySubFolder As folder
Dim myFile As File
Set myFolder = FSO.GetFolder(sPath)
For Each xCell In Selection
For Each mySubFolder In myFolder.subfolders
If InStr(mySubFolder.Name, xCell.Value) > 0 Then
Dim Drive As String
Drive = Left(mySubFolder.Path, 2)
ActiveSheet.Hyperlinks.Add Anchor:=xCell, _
Address:=Replace(mySubFolder.Path, Drive, GetNetworkPath(Drive))
xCell.Offset(0, 1).Value = Now()
Exit For
End If
Recurse = Recurse(mySubFolder.Path)
Next
Next
End Function
VBA Code:
Public Function BrowseFolder()
Dim FldrPicker As FileDialog
Dim myPath As String
'Browse Folder Path
Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker)
With FldrPicker
.Title = "Browse Root Folder Path"
.AllowMultiSelect = False
If .Show <> -1 Then Exit Function
myPath = .SelectedItems(1)
End With
BrowseFolder = myPath
If myPath = vbNullString Then Exit Function
End Function
VBA Code:
Function GetNetworkPath(ByVal DriveName As String) As String
Dim objNtWork As Object
Dim objDrives As Object
Dim lngLoop As Long
Set objNtWork = CreateObject("WScript.Network")
Set objDrives = objNtWork.enumnetworkdrives
For lngLoop = 0 To objDrives.Count - 1 Step 2
If UCase(objDrives.Item(lngLoop)) = UCase(DriveName) Then
GetNetworkPath = objDrives.Item(lngLoop + 1)
Exit For
End If
Next
End Function