excel批量获得文件路径和文件名称并超链接(动态)---详细请看问题补充

2024年11月18日 23:41
有2个网友回答
网友(1):

1. 新建文件,Ait+F11 进入Visual Basic编辑器
2. 工具---引用,找到Microsoft Scripting Runtime,打勾
3. 输入以下代码,运行后保存文件, 每有更改,只需重新运行宏即可。
Sub GetHyperLinks()
Dim MyPath As String, FileCnt As Long
Dim MyFSO As New FileSystemObject, MyFolder As Folder, MyFiles As Files, MyFile As File

MyPath = "E:\电影"
Set MyFolder = MyFSO.GetFolder(MyPath)
Set MyFiles = MyFolder.Files

FileCnt = 0
ActiveSheet.Cells(1, 1) = "文件名"
For Each MyFile In MyFiles
FileCnt = FileCnt + 1
ActiveSheet.Cells(FileCnt + 1, 1) = MyPath & "\" & Trim(MyFile.Name)
ActiveSheet.Cells(FileCnt + 1, 1).Hyperlinks.Add Anchor:=ActiveSheet.Cells(FileCnt + 1, 1), Address:=ActiveSheet.Cells(FileCnt + 1, 1)
Next
End Sub

网友(2):

qq773189637