Macro to prompt for hyperlink path

Chrismar1410

New Member
Joined
Feb 9, 2024
Messages
1
Office Version
  1. 2013
Platform
  1. Windows
Please see my image below - I want to record a macro that allows me to insert a hyperlink but stop before I choose the actual link I want to use as every line will have a different link
 

Attachments

  • Macro - Hyperlink.jpg
    Macro - Hyperlink.jpg
    199.3 KB · Views: 9

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Try this:
VBA Code:
Public Sub LinkCell()
   Dim fso As Object: Set fso = CreateObject("Scripting.FileSystemObject")
   Dim fil As Object
   Dim filename As String
   Dim path As String
   Dim text As String
   Dim diaFile As FileDialog
   Dim col As New Collection
   Dim cell As Range
   col.Add Application.InputBox(Prompt:="Choose cell to add hyperlink", Title:="Select Cell", Type:=8)
   If TypeOf col(1) Is Range Then
      Set cell = col(1)
      Set diaFile = Application.FileDialog(msoFileDialogFilePicker)
      With diaFile
         .Title = "Select file to be linked"
         .AllowMultiSelect = False
      End With
      If diaFile.Show = 0 Then Exit Sub
      Set fil = fso.GetFile(diaFile.SelectedItems.Item(1))
      path = fil.path
      filename = Left(fil.Name, InStrRev(fil.Name, ".") - 1)
      text = Application.InputBox( _
         Prompt:="Please type in the text to be displayed for the link", _
         Title:="Hyperlink Text", _
         Default:=filename)
      If text = False Then text = filename
      cell.Hyperlinks.Add Anchor:=cell, Address:=path, TextToDisplay:=text
   End If
End Sub

Place this Macro in a (new) Module and run it. This is what it does:
  1. Select a cell in a Worksheet (where the Hyperlink should be added)
  2. Select a file (which should be linked to the previously selected cell)
  3. Enter a text (Hyperlink text, that should be displayed in the cell)
Please let me know if this is what you need.
 
Upvote 0

Forum statistics

Threads
1,216,095
Messages
6,128,795
Members
449,468
Latest member
AGreen17

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