Hyperlink on VBA

MSqueries

New Member
Joined
Nov 11, 2024
Messages
2
Office Version
  1. 365
Platform
  1. Windows
I have this module that find all folders and files under the selected folder. The list of files path is generated in the excel sheet from the column B row 11 to infinity. I am trying to add a condition that changes column into an hyperlink for row 11 to infinity in column c. The aim is to be able to click on the cell with the path link to open the file. Greatly appreciate your expertise. The module is as follow:

Dim iRow

Sub ListFiles()
iRow = 11
Call ListMyFiles(Range("C7"), Range("C8"))
End Sub

Sub ListMyFiles(mySourcePath, IncludeSubfolders)
Set MyObject = New Scripting.FileSystemObject
Set Mysource = MyObject.GetFolder(mySourcePath)
On Error Resume Next
For Each myFile In Mysource.Files
iCol = 2
Cells(iRow, iCol).Value = myFile.Path
iCol = iCol + 1
Cells(iRow, iCol).Value = myFile.Name
iCol = iCol + 1
Cells(iRow, iCol).Value = myFile.Size
iCol = iCol + 1
Cells(iRow, iCol).Value = myFile.DateLastModified
iRow = iRow + 1
Next
If IncludeSubfolders Then
For Each mySubFolder In Mysource.SubFolders
Call ListMyFiles(mySubFolder.Path, True)

End If

End Sub
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Welcome to MrExcel.

Replace:
VBA Code:
        Cells(iRow, iCol).Value = myFile.Name
with:
VBA Code:
        ActiveSheet.Hyperlinks.Add Anchor:=Cells(iRow, iCol), Address:=myFile.Path, TextToDisplay:=myFile.Name

PS please use VBA code tags when posting code - click the VBA icon on the message editor toolbar.
 
Upvote 0
Solution
Welcome to MrExcel.

Replace:
VBA Code:
        Cells(iRow, iCol).Value = myFile.Name
with:
VBA Code:
        ActiveSheet.Hyperlinks.Add Anchor:=Cells(iRow, iCol), Address:=myFile.Path, TextToDisplay:=myFile.Name

PS please use VBA code tags when posting code - click the VBA icon on the message editor toolbar.
Dear John,
Thank you so much and lot to learn appreciate your assistance. It work fine.
 
Upvote 0

Forum statistics

Threads
1,223,630
Messages
6,173,456
Members
452,514
Latest member
cjkelly15

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