Enter Hyperlink based on file name

Panoos64

Well-known Member
Joined
Mar 1, 2014
Messages
882
Hi to all of you, I would much appreciate if you could provide me a support so that, to create a VBA code which should run through col. “J” in my list file and where a cell is blank should place the hyperlink based on matching file name with name in col. “F” Therefore that the file name contains creation date at the end so should match the name only. Note that “List file” and “Items cards” are in same folder. Below I present an extract of my list file after the run of code. Thank you all in advance

F G H I J
Item description
Dpt
Item code
Category
Invetory card
Wholemeal slices 1000gr
Restaurant​
40107001​
Bakeries​
Wholemeal slices 1000gr 15.03.2023.xlsx
 
Last edited:

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Sub InsertHyperlink()

Dim listFile As Workbook
Dim listSheet As Worksheet
Dim itemsFolder As String
Dim itemsFile As String
Dim itemRow As Long

Set listFile = ActiveWorkbook 'Change to the name of your list file
Set listSheet = listFile.Sheets("Sheet1") 'Change to the name of your sheet
itemsFolder = listFile.Path & "\" 'Assuming the items files are in the same folder

For itemRow = 2 To listSheet.Cells(Rows.Count, "J").End(xlUp).Row 'Loop through all the rows with inventory cards
If listSheet.Cells(itemRow, "J").Value = "" Then 'Check if the cell is empty
itemsFile = Dir(itemsFolder & "*" & listSheet.Cells(itemRow, "F").Value & "*.xlsx") 'Find the file name with matching date
If itemsFile <> "" Then 'Check if a matching file was found
listSheet.Hyperlinks.Add Anchor:=listSheet.Cells(itemRow, "J"), Address:=itemsFolder & itemsFile 'Insert the hyperlink
End If
End If
Next itemRow

End Sub

This code assumes that your list file is open and active when you run it. If your list file has a different name or your sheet has a different name, you'll need to change the code accordingly. Also, make sure that the items files have the format "Item description creation date.xlsx" and that the date is separated from the description by a space.
 
Upvote 0
Thank you GSP, it works and is based on my data. Thank you for your support too and your time. Hv a great day!
 
Upvote 0

Forum statistics

Threads
1,214,920
Messages
6,122,276
Members
449,075
Latest member
staticfluids

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