VBA Hyperlink file within a folder with File Section OPTION?

FGaxha

Board Regular
Joined
Jan 10, 2023
Messages
227
Office Version
  1. 365
Platform
  1. Windows
Hi, need help?
I have a vba code to hyperlink all file in a folder.
Im looking for vba to to give me the option to select folder location as below.
1676302339443.png


My Hyperlink VBA is:
Sub InsertFilesInFolder()
Dim sPath As String, Value As String
Dim WS As Worksheet
Set WS = Sheets.Add
sPath = ActiveWorkbook.Path & "\"
'Value = Dir(sPath, &H1F)'exctract all kind of files, pdf,text,word
Value = Dir(sPath & "*.xls*", &H1F) 'exctract only excel file
WS.Range("A1") = "Filename"
Set StartCell = WS.Range("A2")
Do Until Value = ""
If Value = "." Or Value = ".." Then
Else
If GetAttr(sPath & Value) = 16 Then
Else
If Value <> ActiveWorkbook.Name And Value <> "~$" & ActiveWorkbook.Name Then
StartCell.Hyperlinks.Add Anchor:=StartCell, Address:= _
Value, TextToDisplay:=Value
Set StartCell = StartCell.Offset(1, 0)
End If
End If
End If
Value = Dir
Loop
End Sub
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
try this.
VBA Code:
Sub InsertFilesInFolder()
Dim sPath As String, value As String
Dim WS As Worksheet
Set WS = Sheets.Add
    With Application.FileDialog(msoFileDialogFolderPicker)
       .Show
        If .SelectedItems.Count > 0 Then
            sPath = .SelectedItems(1) & "\"
        Else
            Exit Sub
        End If
    End With
value = Dir(sPath & "*.xls*", &H1F) 'exctract only excel file
WS.Range("A1") = "Filename"
Set StartCell = WS.Range("A2")
Do Until value = ""
If value = "." Or value = ".." Then
Else
If GetAttr(sPath & value) = 16 Then
Else
If value <> ActiveWorkbook.Name And value <> "~$" & ActiveWorkbook.Name Then
StartCell.Hyperlinks.Add Anchor:=StartCell, Address:= _
sPath & value, TextToDisplay:=value
Set StartCell = StartCell.Offset(1, 0)
End If
End If
End If
value = Dir
Loop
End Sub
 
Upvote 1
Solution
try this.
VBA Code:
Sub InsertFilesInFolder()
Dim sPath As String, value As String
Dim WS As Worksheet
Set WS = Sheets.Add
    With Application.FileDialog(msoFileDialogFolderPicker)
       .Show
        If .SelectedItems.Count > 0 Then
            sPath = .SelectedItems(1) & "\"
        Else
            Exit Sub
        End If
    End With
value = Dir(sPath & "*.xls*", &H1F) 'exctract only excel file
WS.Range("A1") = "Filename"
Set StartCell = WS.Range("A2")
Do Until value = ""
If value = "." Or value = ".." Then
Else
If GetAttr(sPath & value) = 16 Then
Else
If value <> ActiveWorkbook.Name And value <> "~$" & ActiveWorkbook.Name Then
StartCell.Hyperlinks.Add Anchor:=StartCell, Address:= _
sPath & value, TextToDisplay:=value
Set StartCell = StartCell.Offset(1, 0)
End If
End If
End If
value = Dir
Loop
End Sub
Thank you so much for your help.
This is great.
 
Upvote 0

Forum statistics

Threads
1,223,239
Messages
6,170,947
Members
452,368
Latest member
jayp2104

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