|
- Sub GetExcelFiles()
- Dim selectedFolder As String
- selectedFolder = GetFolder()
- If selectedFolder = "" Then Exit Sub
-
- Dim folderPath As String
- folderPath = selectedFolder & ""
-
- Dim fso As Object
- Set fso = CreateObject("Scripting.FileSystemObject")
-
- Dim wb As Workbook
- Set wb = ThisWorkbook
-
- Dim ws As Worksheet
- Set ws = wb.Sheets("Sheet1")
-
- Dim lastRow As Long
- lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
- Dim fileIndex As Long
- If lastRow > 1 Then
- fileIndex = ws.Cells(lastRow, "A").Value + 1
- End If
-
- Dim file As Object
- Dim fileName As String
- Dim filePath As String
- Dim linkAddress As String
-
- For Each file In fso.GetFolder(folderPath).Files
- If LCase(Right(file.Name, 4)) = ".xls" Or LCase(Right(file.Name, 5)) = ".xlsx" Then
- fileName = file.Name
- filePath = file.Path
- linkAddress = "=HYPERLINK(""" & filePath & """, """ & fileName & """)"
- ws.Cells(lastRow + 1, "A").Value = fileIndex
- ws.Cells(lastRow + 1, "B").Value = fileName
- ws.Cells(lastRow + 1, "C").Value = folderPath
- ws.Cells(lastRow + 1, "D").Formula = linkAddress
- lastRow = lastRow + 1
- fileIndex = fileIndex + 1
- End If
- Next file
-
- MsgBox "已完成获取 " & fileIndex - 1 & " 个工作簿文件名的操作。"
- End Sub
- Function GetFolder()
- With Application.FileDialog(msoFileDialogFolderPicker)
- .Title = "请选择一个文件夹"
- .Show
- If .SelectedItems.Count > 0 Then
- GetFolder = .SelectedItems(1)
- End If
- End With
- End Function
复制代码
|
|