sspatriots
Well-known Member
- Joined
- Nov 22, 2011
- Messages
- 585
- Office Version
- 365
- Platform
- Windows
I'm trying to use the code below to create a hyperlink on a cell that is selected when the dialog box pops up. It will create a hyperlink, but replaces the cell contents with the entire link path. I tried using
fName = ActiveCell.Value 'Mid(fPath, InStrRev(fPath, "") + 1, 9999), but that only works if I have that cell activated before I run the macro. I'd like the link name be the cell value that was originally in the cell. The only other issue I am having with this code is if I run the macro and try and cancel out from the pop-up dialog box, I get the error message below the code shown below.
Sub CreateHyperLink()
Dim fd As Object, fPath As String, fName As String, cel As Range
Set cel = Application.InputBox("Select a cell", "Add Link to File", , , , , , 8)
Set fd = Application.FileDialog(msoFileDialogFilePicker)
'Set selectedCell = Application.ActiveCell
With fd
.InitialFileName = " " 'default folder path - MUST end with \
.AllowMultiSelect = False
.Title = "Please select a file."
.Filters.Clear
If .Show = True Then
fPath = fd.SelectedItems(1)
fName = 'Mid(fPath, InStrRev(fPath, "") + 1, 9999)
Else
Exit Sub
End If
End With
cel.Hyperlinks.Add Anchor:=cel, Address:=fPath, TextToDisplay:=fName
End Sub
Any suggestions would be greatly appreciated.
Thanks,
SS
fName = ActiveCell.Value 'Mid(fPath, InStrRev(fPath, "") + 1, 9999), but that only works if I have that cell activated before I run the macro. I'd like the link name be the cell value that was originally in the cell. The only other issue I am having with this code is if I run the macro and try and cancel out from the pop-up dialog box, I get the error message below the code shown below.
Sub CreateHyperLink()
Dim fd As Object, fPath As String, fName As String, cel As Range
Set cel = Application.InputBox("Select a cell", "Add Link to File", , , , , , 8)
Set fd = Application.FileDialog(msoFileDialogFilePicker)
'Set selectedCell = Application.ActiveCell
With fd
.InitialFileName = " " 'default folder path - MUST end with \
.AllowMultiSelect = False
.Title = "Please select a file."
.Filters.Clear
If .Show = True Then
fPath = fd.SelectedItems(1)
fName = 'Mid(fPath, InStrRev(fPath, "") + 1, 9999)
Else
Exit Sub
End If
End With
cel.Hyperlinks.Add Anchor:=cel, Address:=fPath, TextToDisplay:=fName
End Sub
Any suggestions would be greatly appreciated.
Thanks,
SS