I am trying to get a macro to hyperlink to a folder while checking to see if a folder exists for the hyperlinked item. My problem is that the first DIR$ call is unreliable. Sometimes it works as written while other times it fails with a Runtime error '76' Path not found.
Why would it work sometimes but not others? This is my first attempt using the DIR Function.
Here's a link to the folder structure and the XLSM File project. Any help would be greatly appreciated.
https://1drv.ms/u/s!AtvY5ZcCa165hLMithqXu8YsNcl3Gg
Thank you.
Why would it work sometimes but not others? This is my first attempt using the DIR Function.
Here's a link to the folder structure and the XLSM File project. Any help would be greatly appreciated.
https://1drv.ms/u/s!AtvY5ZcCa165hLMithqXu8YsNcl3Gg
Thank you.
Code:
Sub Macro1()
Dim strFile As String
' Gets column to hyperlink
Set Rng = Application.InputBox(Prompt:="Select cells to hyperlink.", Title:="Generate Effort Hyperlinks", Type:=8)
' Sets the folder name for the OAWRs, RFPs, HSIs, LGs, PROPs, etc.
strFile = Dir$("..\*Efforts", vbDirectory) '##### THIS RETURNS EMPTY... SOMETIMES ####
' Iterates through cells to generate hyperlink
For Each cell In Rng
' Selects cell to manipulate
cell.Select
With Selection
.Hyperlinks.Delete ' Deletes existing Hyperlink
' Checks for existing folder as loop; creates Effort Folders if not found
Do While Len(Dir$("..\" & strFile & "\" & Mid(.Offset(0, 1), 1, Len(.Offset(0, 1).Value) - 3) & "\" & .Offset(0, 1).Value & "*", vbDirectory)) = 0
MkDir "..\" & strFile & "\" & Mid(.Offset(0, 1), 1, Len(.Offset(0, 1).Value) - 3) & "\" & .Offset(0, 1).Value _
& " (" & .Value & ") " & .Offset(0, 3)
' Select CASE sets up different folders for efforts
Select Case Mid(.Offset(0, 1), 1, Len(.Offset(0, 1).Value) - 3)
Case "RFP"
MkDir "..\" & strFile & "\" & Mid(.Offset(0, 1), 1, Len(.Offset(0, 1).Value) - 3) & "\" & .Offset(0, 1).Value _
& " (" & .Value & ") " & .Offset(0, 3) & "\Proposal"
Case Else
MkDir "..\" & strFile & "\" & Mid(.Offset(0, 1), 1, Len(.Offset(0, 1).Value) - 3) & "\" & .Offset(0, 1).Value _
& " (" & .Value & ") " & .Offset(0, 3) & "\Package"
End Select
' Creates default ATP-WA folder
MkDir "..\" & strFile & "\" & Mid(.Offset(0, 1), 1, Len(.Offset(0, 1).Value) - 3) & "\" & .Offset(0, 1).Value _
& " (" & .Value & ") " & .Offset(0, 3) & "\ATP-WA"
Loop
' Searches for the Directory with the Effort number. Example, "OAWR001" or "RFP001" and sets the strFile variable to the folder name found
strHyper = Dir$("..\" & strFile & "\" & Mid(.Offset(0, 1), 1, Len(.Offset(0, 1).Value) - 3) & "\" & .Offset(0, 1).Value & "*", vbDirectory)
' Sets the hyperlink to the path of the folder
.Hyperlinks.Add Anchor:=Selection, _
Address:="..\" & strFile & "\" & Mid(.Offset(0, 1), 1, Len(.Offset(0, 1).Value) - 3) & "\" & strHyper
End With
Next cell
End Sub