'Excel VBA module to test if hyperlink string refers to valid file
I am trying to figure out a way to test if a hyperlink string in excel points to a valid file on our network. But my limited VBA knowledge seems to be getting the better of me.
Example: The hypothetical function "HyperTest" returns TRUE or FALSE depending on Hyperlink validity. Cell A1 Contains "c:\123.txt" Cell B1 Contains "=IF(HyperTest(A1),"Yay, the file is real!","Looks like somebody deleted your file.")
I assume the module code should resemble something like:
Public Function HyperTest(hyperpath As String) As Boolean
If [insert test here] Then
return TRUE
Else
return FALSE
End If
End Function
Solution 1:[1]
You can use the Dir() function. If the path doesn't exist, the function doesn't return anything. And since the default value of a boolean is False, you don't need an Else statement.
Public Function HyperTest(hyperpath As String) As Boolean
If Dir(hyperpath) <> vbNullString Then HyperTest = True
End Function
Solution 2:[2]
you can use this small UDF to get your OP
Function HyperTest(c As Range)
If Dir(c) <> "" Then
HyperTest = "File exists."
Else
HyperTest = "File doesn't exist."
End If
End Function
and call this function in any cell
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | K.Dᴀᴠɪs |
| Solution 2 | Karthick Gunasekaran |

