Add a hyperlink to a cell

LogicalHammer1973

New Member
Joined
Apr 21, 2020
Messages
4
Office Version
  1. 365
Platform
  1. Windows
Hi,

Can someone help, i'm relatively new at VBA coding and have got stuck.

Im trying to add a hyperlink to a PDF file created in the VBA macro to a cell in row F (tried sh.range ("F" & n +1).value = hyperlink etc, but no joy ), I've got the code as follows, but cant seem to workout how to create the hyperlink? I would like to name the hyperlink after the file name created for the PDF.

extract of working code so far:

Sheets("Near Miss").Range("A1:N47").ExportAsFixedFormat Type:=xlTypePDF, filename:= _
"c:\my documents\" & ComboBox1.Value & "_Near Miss_" & Format(Now, "dd_mm_YY_hh_mm") & ".pdf", Quality:= _
xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
OpenAfterPublish:=False

Workbooks.Open "C:\Users\mulhalla\Documents\KC Site Near Miss Reports.xlsm"
Set sh = Workbooks("KC Site Near Miss Reports").Sheets("Near-Miss")
n = sh.Range("B" & Application.Rows.Count).End(xlUp).Row
sh.Range("B" & n + 1).Value = Me.ComboBox1.Value
sh.Range("C" & n + 1).Value = Me.TextBox1.Value
sh.Range("D" & n + 1).Value = Me.TextBox2.Value
sh.Range("E" & n + 1).Value = Me.TextBox3.Value
sh.Range("H" & n + 1).Value = "No"
Workbooks("KC Site Near Miss Reports").Close SaveChanges:=True

If anyone can help that would be much appreciated,

PS Sorry if coding isn't great.
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Just add a line like this...

VBA Code:
Sheet1.Range("A1").Hyperlinks.Add Address:="C:\Documents\mypath.extension", TextToDisplay:="The Name You Want"
 
Upvote 0
VBA Code:
sh.range ("F" & n +1) .Hyperlinks.Add Address:="C:\Documents\mypath.extension", TextToDisplay:="The Name You Want"

In your case.
 
Upvote 0
Hi COwen,

Many thanks for the replay, I've tried as you suggested but get an error 450 (wrong number of arguments or invalid property assignment) with the modified code as below:

Worksheets("Near Miss").Visible = True
Sheets("Near Miss").Range("A1:N47").ExportAsFixedFormat Type:=xlTypePDF, filename:= _
"c:\my documents\" & ComboBox1.Value & "_Near Miss_" & Format(Now, "dd_mm_YY_hh_mm") & ".pdf", Quality:= _
xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
OpenAfterPublish:=False
pdfile = "c:\my documents\" & ComboBox1.Value & "_Near Miss_" & Format(Now, "dd_mm_YY_hh_mm") & ".pdf"
Worksheets("Near Miss").Visible = False
Workbooks.Open "C:\Users\mulhalla\Documents\KC Site Near Miss Reports.xlsm"
Set sh = Workbooks("KC Site Near Miss Reports").Sheets("Near-Miss")
n = sh.Range("B" & Application.Rows.Count).End(xlUp).Row
sh.Range("B" & n + 1).Value = Me.ComboBox1.Value
sh.Range("C" & n + 1).Value = Me.TextBox1.Value
sh.Range("D" & n + 1).Value = Me.TextBox2.Value
sh.Range("E" & n + 1).Value = Me.TextBox3.Value
sh.Range("F" & n + 1).Hyperlinks.Add Address:=(pdfile), TextToDisplay:="Link"
sh.Range("H" & n + 1).Value = "No"

When i hover over the pdfile in address is pointing to the file created.

Any ideas?
 
Upvote 0
Where is it throwing the error?
 
Upvote 0
Try
VBA Code:
With sh.Range("F" & N + 1)
   .Hyperlinks.Add .Offset(0, 0), pdfile, "", , "Link"
End With
 
Upvote 0
Glad we could help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,561
Members
449,089
Latest member
Motoracer88

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top