Sub 按钮1_Click()
Application.ScreenUpdating = False
Set fso = CreateObject("scripting.filesystemobject")
Set ff = fso.getfolder(ThisWorkbook.Path) 'ThisWorkbook.Path是当前代码文件所在路径,路径名可以根据需求修改
ActiveSheet.UsedRange.ClearContents
a = 1
For Each f In ff.Files
Rem 如果不需要提取本代码文件名,可以增加if语句 if f.name<>thisworkbook.name then.....
Rem 如果值需要提取某类文件,需要对f.name的扩展名进行判断
Rem 个人感觉split取 扩展名:split(f.name,".")(ubound(split(f.name,"."))),然后再判断,避免文件名还有其他“.”
Cells(a, 1) = f.Name '相对路径名
Cells(a, 2) = f '全路径名
a = a + 1
Next f
Application.ScreenUpdating = True
End Sub
excel中用vba实现自动提取文件夹内的文件名的方法如下:
1、新建一个vba宏脚本
2、写入如下代码:
Function GetFileList(FileSpec As String) As Variant
' Returns an array of filenames that match FileSpec
' If no matching files are found, it returns False
Dim FileArray() As Variant
Dim FileCount As Integer
Dim FileName As String
On Error GoTo NoFilesFound
FileCount = 0
FileName = Dir(FileSpec)
If FileName = "" Then GoTo NoFilesFound
' Loop until no more matching files are found
Do While FileName <>""
FileCount = FileCount + 1
ReDim Preserve FileArray(1 To FileCount)
FileArray(FileCount) = FileName
FileName = Dir()
Loop
GetFileList = FileArray
Exit Function
' Error handler
NoFilesFound:
GetFileList = False
End Function
3、传入文件路径就可以获取文件名到指定的excel表格中
4、结果:
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)