Add a hyperlink and display click here to open

josros60

Well-known Member
Joined
Jun 27, 2010
Messages
786
Office Version
  1. 365
Hi,

How can I modify the code below to add besides file name, last date modify and the next column to add hyperlink to the file and display "click here to open"

VBA Code:
Sub ListFilesContainingSearchTerms()

    Dim folderName As String
    With Application.FileDialog(msoFileDialogFolderPicker)
        .Title = "Select a Folder"
        .InitialFileName = Application.DefaultFilePath & "\" 'change the initial folder as desired
        If .Show = 0 Then Exit Sub 'user cancelled
        folderName = .SelectedItems(1)
    End With
   
    Dim wrd As String
    wrd = InputBox("Insert one or more search terms, separated by a semi-colom (ie. blue;red;green).", "Search Terms")
    If wrd = "" Then
        MsgBox "???", vbQuestion, "Search Terms"
        Exit Sub
    End If
   
    Cells.ClearContents 'clear contents of active worksheet for new list of files
   
    Dim searchTerms As Variant
    searchTerms = Split(Trim(wrd), ";") 'Trim() to remove any leading and trailing spaces, and Split() to split the string into an array
   
    Dim startRow As Long
    startRow = 1 'change the starting row as desired
   
    Dim fileCount As Long
    fileCount = 0
   
    Dim fso As Object
    Set fso = CreateObject("Scripting.FileSystemObject")
   
    ProcessFolder fso, folderName, True, searchTerms, startRow, fileCount

    MsgBox "Number of files that match the search terms: " & fileCount, vbInformation, "File Count"
 
    Set fso = Nothing
   
End Sub

VBA Code:
Private Sub ProcessFolder(ByVal fso As Object, ByVal folderName As String, ByVal includeSubfolders As Boolean, ByRef searchTerms As Variant, ByRef rowNumber As Long, ByRef fileCount As Long)

    Dim currentFolder As Object
    Dim currentSubFolder As Object
    Dim currentFile As Object
   
    Set currentFolder = fso.GetFolder(folderName)
   
    For Each currentFile In currentFolder.Files
        If isMatchFound(currentFile.Name, searchTerms) Then
            Cells(rowNumber, "A").Value = currentFile.Name
            Cells(rowNumber, "B").Value = currentFile.DateLastModified
            FileDate = Format(currentFile.DateLastModified, "YYYYMMDD")
            rowNumber = rowNumber + 1
            fileCount = fileCount + 1
        End If
    Next currentFile
   
    If includeSubfolders Then
        For Each currentSubFolder In currentFolder.subfolders
            ProcessFolder fso, currentSubFolder, True, searchTerms, rowNumber, fileCount
        Next currentSubFolder
    End If
   
End Sub


VBA Code:
Private Function isMatchFound(ByVal fileName As String, ByVal searchTerms As Variant) As Boolean

    Dim i As Long
   
    For i = LBound(searchTerms) To UBound(searchTerms)
        If UCase(fileName) Like "*" & UCase(searchTerms(i)) & "*" Then 'case-insensitive match
            isMatchFound = True
            Exit Function
        End If
    Next i
   
    isMatchFound = False

End Function


Thank you
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Try the following . . .

VBA Code:
ActiveSheet.Hyperlinks.Add Anchor:=Cells(rowNumber, "C"), Address:=currentFile.Path, TextToDisplay:="click here to open"

Hope this helps!
 
Upvote 0
Solution

Forum statistics

Threads
1,223,888
Messages
6,175,203
Members
452,617
Latest member
Narendra Babu D

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