I am relatively new to VBA programming but am trying to pick up quickly.
Basically I have been online and found some code as I want to click on the VBA button and it load up the file path explorer and when the file is chosen I want to put it into a particular cell.
Code I currently have is (and I cannot take credit for) is:-
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)
With fd
.InitialFileName = Environ(“USERPROFILE”) & “” ‘default folder path – MUST end with[COLOR=#ff0000] [B][/B][/COLOR]
.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
Problems I have with this is:-
-
It loads up a window that asks which cell I want to put the hyperlink into. The cell I want it to do this into is on a worksheet called “Summary” and in cell G7.
-
How do I get it to always load a specific folder, when the file path explorer opens it defaults to:
C:Usersxxxxx – I would like it to default to: C:Usersxxxxxxxxxxinvoices. -
When it returns the file link in cell G7 I do not want it to return the file path in the cell I want it to show the file name e.g. Invoice123 not C:UsersxxxxxxxxxxinvoicesInvoice123.xlsx
I hope that all makes sense
Thanks very much in advance
I tried the above code but it brought up the box asking where I wanted to input the hyperlink and also shows the hyperlink not the file name.
Croon is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.