Excel宏以获取特定文件夹修改中文件的链接列表

时间:2017-10-03 21:43:19

标签: excel vba excel-vba

我有一个允许用户选择文件夹的宏,并将该文件夹中的文件列为链接。但是,列表从单元格A1开始,但是说,我单击了单元格I5,我希望列表从我单击的单元格开始。如何修改以下宏:

Sub GetFileNames()

Dim xFSO As Object
Dim xFolder As Object
Dim xFile As Object
Dim xFiDialog As FileDialog
Dim xPath As String
Dim I As Integer
Set xFiDialog = Application.FileDialog(msoFileDialogFolderPicker)
If xFiDialog.Show = -1 Then
    xPath = xFiDialog.SelectedItems(1)
End If
Set xFiDialog = Nothing
If xPath = "" Then Exit Sub
Set xFSO = CreateObject("Scripting.FileSystemObject")
Set xFolder = xFSO.GetFolder(xPath)
For Each xFile In xFolder.Files
    I = I + 1
    ActiveSheet.Hyperlinks.Add Cells(I, 1), xFile.Path, , , xFile.Name
Next
End Sub

非常感谢您的专业知识!!!!

1 个答案:

答案 0 :(得分:1)

将此内容加入到您的宏中:

Dim curCell As Range
Set curCell = ActiveCell
i = 0
For Each xFile In xFolder.Files
    ActiveSheet.Hyperlinks.Add Cells(curCell.Row + i, curCell.Column), xFile.Path, , , xFile.Name
    i = i + 1
Next

运行宏时,它会查看活动单元格是什么,然后使用该列和行开始。