如何将文件夹里的内容通过VBA在EXCEL中添加超链接
时间: 2024-05-27 14:11:10 浏览: 9
你可以使用以下代码将文件夹中的所有文件添加为超链接:
Sub AddHyperlinksToFilesInFolder()
Dim objFSO As Object
Dim objFolder As Object
Dim objFile As Object
Dim strFolderPath As String
Dim strFilePath As String
Dim intLastRow As Integer
'Get folder path from user
strFolderPath = InputBox("Enter Folder Path")
'Create FileSystemObject
Set objFSO = CreateObject("Scripting.FileSystemObject")
'Get folder object
Set objFolder = objFSO.GetFolder(strFolderPath)
'Loop through each file in folder
For Each objFile In objFolder.Files
'Get file path
strFilePath = objFile.Path
'Add hyperlink to file in Excel
ActiveSheet.Hyperlinks.Add Anchor:=Cells(intLastRow + 1, 1), Address:=strFilePath, TextToDisplay:=objFile.Name
'Increment row counter
intLastRow = intLastRow + 1
Next objFile
End Sub
请注意,此代码将在活动工作表的第一列中添加超链接。您可以根据需要更改此行为。此外,请确保已正确引用Microsoft Excel对象库。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![xlam](https://img-home.csdnimg.cn/images/20210720083646.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)