想要从文件夹中获取文件名并使用相同的文件创建超链接

时间:2013-12-13 10:45:16

标签: excel-vba vba excel

我想从文件夹中获取所有文件名,并使用相同的文件创建超链接。

下面提到的是我的代码请协助。

Sub FileName1()
Dim I As Integer
Dim fl As File
Dim fldr As Folder
Dim SH As Worksheet
Dim fso As FileSystemObject
Dim Fpath As String
Dim mpath As String
With Application.FileDialog(msoFileDialogFolderPicker)
.Show
Fpath = .SelectedItems(1)
mpath = Fpath & "\"
End With

I = 1
Set fso = New FileSystemObject
Set fldr = fso.GetFolder(Fpath)

For Each fl In fldr.Files
First.Cells(I, 1) = fl.Name
If First.Cells(I, 1) = fl.Name Then
First.Cells(I, 1).Select

First.Hyperlinks.Add Anchor:=First.Cells(I, 1), Address:=mpath,  TextToDisplay:=First.Cells(I, 1)

End If
I = I + 1
Next fl
End Sub

1 个答案:

答案 0 :(得分:0)

更改

First.Hyperlinks.Add Anchor:=First.Cells(I, 1), Address:=mpath,  TextToDisplay:=First.Cells(I, 1)

First.Hyperlinks.Add Anchor:=First.Cells(I, 1), Address:=mpath & fl.Name,  TextToDisplay:=First.Cells(I, 1).Value
相关问题