Hi all,
1. I have built created a code that when I right click on certain cells it will allow me to browse for a file using the windows browser, when selected - the name of the file is pasted into the cell I right clicked on and creates a link to the file so I can simply just click on the link and it will take me to the file directly.
2. What I want now is, if for some reason I change the file location the link will no longer work. My idea is to have the ability to have a separate macro that will allow me to select the master folder that these files come from, so all I have to do is alter the master folder path and the links will still work (given the file names aren't changed, which they won't be).
The difficult part is that these files will be stored on a network drive as multiple people will require access to these files, however, not everyone will have these mapped to the same drives hence why I require the UNC path.
Here is what my code looks like to achieve the first part:
1. I have built created a code that when I right click on certain cells it will allow me to browse for a file using the windows browser, when selected - the name of the file is pasted into the cell I right clicked on and creates a link to the file so I can simply just click on the link and it will take me to the file directly.
2. What I want now is, if for some reason I change the file location the link will no longer work. My idea is to have the ability to have a separate macro that will allow me to select the master folder that these files come from, so all I have to do is alter the master folder path and the links will still work (given the file names aren't changed, which they won't be).
The difficult part is that these files will be stored on a network drive as multiple people will require access to these files, however, not everyone will have these mapped to the same drives hence why I require the UNC path.
Here is what my code looks like to achieve the first part:
Code:
Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)
Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
If Not Intersect(Target, Range("F5:F1000")) Is Nothing Then 'If user right click's on any cell within the range F5 to F1000 then
'this subroutine will run
MyFileLocation = Application.GetOpenFilename() 'Opens up windows browser and allows user to browse for required file
Filename = Split(MyFileLocation, "\")(UBound(Split(MyFileLocation, "\"))) 'Saves the selected file name
filePath = Left(MyFileLocation, InStrRev(MyFileLocation, "\")) 'Saves the file path
fDialog.InitialFileName = filePath 'Sets the file path as the initial location in the windows browser
If MyFileLocation = False Then 'If no file is selected or cancel is selected
Cancel = True
Exit Sub 'then exit sub, aka do nothing
Else
Target = Filename 'If a file is selected then the target value will be the file name
End If
Cancel = True
If Selection <> "" Then 'If selection is not blank then create a hyperlink to the file
'Otherwise do nothing
With Target
.Hyperlinks.Add Anchor:=Selection, _
Address:=MyFileLocation, _
TextToDisplay:=Filename
End With
Else
End If
End If
End Sub