Here is my approach. I did not turn off and on the Application items to speed it up as Dan did not nor did I resize columns nor allow user to select the parent folder's path.
Obviously, you need to change "x:\t" to your path.
Sub Main()
Dim x() As Variant, i As Long, c As Range, cc As Range
x() = aFFs("x:\t", "/ad") 'Search for folders in x:\t.
'MsgBox Join(x(), vbLf)
For Each c In Range("A2", Range("A" & Rows.Count).End(xlUp))
Set cc = c.Offset(, 2)
i = IndexP(x(), c.Value2)
cc.ClearContents
If i > -1 Then ActiveSheet.Hyperlinks.Add Anchor:=cc, Address:=x(i), TextToDisplay:=CStr(c.Value2)
Next c
End Sub
'Set extraSwitches, e.g. "/ad", to search folders only.
'MyDir should end in a "\" character unless searching by wildcards, e.g. "x:\test\t*
'Command line switches for the shell's Dir, http://ss64.com/nt/dir.html
Function aFFs(myDir As String, Optional extraSwitches = "", _
Optional tfSubFolders As Boolean = False) As Variant
Dim s As String, a() As String, v As Variant
Dim b() As Variant, i As Long
If tfSubFolders Then
s = CreateObject("Wscript.Shell").Exec("cmd /c dir " & _
"""" & myDir & """" & " /b /s " & extraSwitches).StdOut.ReadAll
Else
s = CreateObject("Wscript.Shell").Exec("cmd /c dir " & _
"""" & myDir & """" & " /b " & extraSwitches).StdOut.ReadAll
End If
a() = Split(s, vbCrLf)
If UBound(a) = -1 Then
MsgBox myDir & " not found.", vbCritical, "Macro Ending"
Exit Function
End If
ReDim Preserve a(0 To UBound(a) - 1) As String 'Trim trailing vblfcr
For i = 0 To UBound(a)
If Not tfSubFolders Then
s = Left$(myDir, InStrRev(myDir, "\"))
'add the folder name
a(i) = s & a(i)
End If
Next i
aFFs = sA1dtovA1d(a)
End Function
Function sA1dtovA1d(strArray() As String) As Variant
Dim varArray() As Variant, i As Long
ReDim varArray(LBound(strArray) To UBound(strArray))
For i = LBound(strArray) To UBound(strArray)
varArray(i) = CVar(strArray(i))
Next i
sA1dtovA1d = varArray()
End Function
'Return index number of a match to last element of delimited string in an array
Function IndexP(vArray() As Variant, val As Variant, _
Optional tfCaseSensitive As Boolean = False, Optional delim As String = "\") As Long
Dim x() As String, i As Long
For i = LBound(vArray) To UBound(vArray)
x() = Split(CStr(vArray(i)), delim)
Select Case True
Case tfCaseSensitive = True
If x(UBound(x)) = val Then
IndexP = i
Exit Function
End If
Case Else
If LCase(x(UBound(x))) = LCase(val) Then
IndexP = i
Exit Function
End If
End Select
Next i
IndexP = -1
End Function
Bookmarks