VBA & Macro Problems

How to Match an Excel Cell Value to a File Name and Add Folder Path

Anum MushtaqAnum Mushtaq Nov 10, 2026 868 views

Question details

The user wants to match an Excel cell's value to an existing file name and generate a clickable hyperlink containing the file's folder path in the adjacent cell.

How to Match an Excel Cell Value to a File Name and Create a Hyperlink
Product
Excel
Device & OS
not provided
Scenario
Automating the creation of file hyperlinks in a spreadsheet based on cell values representing file names.
Observed behavior
The user needs a programmatic method to dynamically link cells to local files without manually inserting each hyperlink.
Before you start

Ensure you have the exact directory path where your files are stored. Additionally, remember to save your workbook as a Macro-Enabled Workbook (.xlsm) and enable macros so the VBA code can execute properly.

Solution 1Recommended

Use VBA to Match Cell Values and Add Hyperlinks

Write a VBA macro to iterate through your cells, find matching file names in a specified folder, and insert hyperlinks.

Using VBA is the most efficient way to automate the process of matching cell values to external files. The script will take the value of each cell, combine it with a base folder path to locate the target file, and then generate a clickable hyperlink in the cell directly adjacent to it.

1
Open the VBA Editor

Press Alt + F11 in your open Excel workbook to launch the VBA Editor.

2
Insert a New Module

Click on 'Insert' in the top menu and select 'Module' to create a blank script window.

3
Input the Hyperlink Code

Write your loop to iterate through the cells. Inside the loop, add the hyperlink creation code: Sheet1.Hyperlinks.Add Anchor:=cell.Offset(,1), Address:=filePath, TextToDisplay:=cell.Value.

4
Define the File Path

Ensure you correctly define the filePath variable by concatenating your directory path with the cell.Value and the appropriate file extension (e.g., filePath = "C:\MyFolder\" & cell.Value & ".pdf").

5
Run the Macro

Press F5 or click 'Run' > 'Run Sub/UserForm' from the top menu to execute the script and generate the hyperlinks.

Use VBA to Match Cell Values and Add Hyperlinks
Check File Extensions: Make sure the cell values either include the correct file extension, or remember to append the extension programmatically within your VBA code before assigning the filePath variable.

Automate Hyperlinks and Macros Easily with WPS Office

WPS Office fully supports VBA macros, allowing you to seamlessly run scripts that match cell values to file paths and generate hyperlinks without any compatibility issues.

  1. 1. Open your Workbook: Launch WPS Spreadsheet and open your existing .xlsm or .xlsx file.
  2. 2. Access the VBA Editor: Navigate to the 'Developer' tab and click on 'VB Editor' to open the macro environment.
  3. 3. Run the Hyperlink Script: Insert your hyperlink VBA code and click 'Run' to instantly generate file paths next to your cells.
Full compatibility with Microsoft Excel formats, including .xlsx, .xls, and .xlsm.Built-in VBA editor to write, debug, and execute spreadsheet macros.Free, lightweight, and fast alternative for handling large datasets and automation.
microsoft office alternative - wps office

Frequently Asked Questions

Why is my VBA hyperlink giving a 'Cannot open the specified file' error?

This usually happens if the filePath generated in VBA is incorrect. Verify that your base folder path ends with a backslash (\) and that the file extension is correctly appended to the cell value.

Can I link to files on a shared network drive instead of a local folder?

Yes. In your VBA code, simply set the folder path to the UNC path of the network drive (e.g., \\Server\SharedFolder\) instead of a local drive letter like C:\.

Does WPS Spreadsheet support the Hyperlinks.Add VBA method?

Yes, WPS Spreadsheet has robust VBA support and perfectly handles standard Excel VBA methods like Hyperlinks.Add to automate tasks.

How do I make the hyperlink display custom text instead of the long file path?

In your VBA code, use the TextToDisplay argument within the Hyperlinks.Add method. Setting it to cell.Value will display the original cell's text instead of the underlying file path.