How to Match an Excel Cell Value to a File Name and Add Folder Path
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.

- 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.
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.
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.
Press Alt + F11 in your open Excel workbook to launch the VBA Editor.
Click on 'Insert' in the top menu and select 'Module' to create a blank script window.
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.
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").
Press F5 or click 'Run' > 'Run Sub/UserForm' from the top menu to execute the script and generate the hyperlinks.

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. Open your Workbook: Launch WPS Spreadsheet and open your existing .xlsm or .xlsx file.
- 2. Access the VBA Editor: Navigate to the 'Developer' tab and click on 'VB Editor' to open the macro environment.
- 3. Run the Hyperlink Script: Insert your hyperlink VBA code and click 'Run' to instantly generate file paths next to your cells.

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.




