Excel vba获取文件夹内文件名
来源:http://www.360doc.com/content/13/1225/16/1086327_340041443.shtml
Sub test()
Dim strFolder As String
Dim varFileList As Variant
Dim FSO As Object, myFile As Object
Dim myResults As Variant
Dim l As Long
'显示打开文件夹对话框
With Application.FileDialog(msoFileDialogFolderPicker)
.Show
If .SelectedItems.Count = 0 Then Exit Sub '未选择文件夹
strFolder = .SelectedItems(1)
End With
'获取文件夹中的所有文件列表
varFileList = fcnGetFileList(strFolder)
If Not IsArray(varFileList) Then
MsgBox "未找到文件", vbInformation
Exit Sub
End If
For x = 0 To UBound(varFileList)
Cells(x + 1, 1) = varFileList(x)
Next x
End Sub
Private Function fcnGetFileList(ByVal strPath As String, Optional strFilter As
String) As Variant
' 将文件列表放到数组
Dim f As String
Dim i As Integer
Dim FileList() As String
If strFilter = "" Then strFilter = "*.*"
Select Case Right(strPath, 1)
Case "\", "/"
strPath = Left(strPath, Len(strPath) - 1)
End Select
ReDim Preserve FileList(0)
f = Dir(strPath & "\" & strFilter)
Do While Len(f) > 0
ReDim Preserve FileList(i) As String
FileList(i) = f
i = i + 1
f = Dir()
Loop
If FileList(0) <> Empty Then
fcnGetFileList = FileList
Else
fcnGetFileList = False
End If
End Function
转载于:https://www.cnblogs.com/panli-32/p/9169408.html
- 点赞
- 收藏
- 分享
- 文章举报
- VB6上创建金蝶K/3或KIS旗舰版插件
- vbs脚本大全
- vbs脚本大全2
- VBox关闭dhcp
- [转载] VBScript 编程参考
- VBS脚本
- VB网页外链接抓取分析软件UrlX
- Visual Basic编程基础
- VB6.0萌新告急!!!
- VBA Code 创建与删除工具栏 -- 测试博客
- vb.net PictureBox包含的图片的大小
- 简易vbs脚本实现在浏览器自动刷新网页。
- vbscript错误代码及对应解释大全/VBScript 语法错误
- VB.net与C#转换网址
- vb,wps,excel 提取括号的数字
- vb 案例学习
- vba,自定义公式,农历互转公历,excel ,wps
- vba控制图表,excel图表,一键完成
- vba 两个表 信息合体一个表格
- 利用nexus2.14搭建mavbe私服