Excel2007中用DIR函数批量获取指定目录下所有文件名
时间:2023-03-06 19:23:43
由于 Excel2007 及 Excel2010 版本都取消了对 Application 对象的 FileSearch 方法的支持,所以在 Excel2007 版本以后不能用 FileSearch 来批量获取指定目录下的所有文件名了,虽然少了 FileSearch 但还可以用内置的 Dir 函数。代码如下:
Sub listfile()
””””””””””””””””””””””’
‘ Dir函数批量获取指定目录下所有文件名和内容 ‘
‘ ‘
””””””””””””””””””””””’
Dim mypath As String, nm As String
Dim theSh As Object
Dim theFolder As Object
Dim i As Integer
Application.ScreenUpdating = False
On Error Resume Next
‘设置搜索路径
Set theSh = CreateObject("shell.application")
Set theFolder = theSh.BrowseForFolder(0, "", 0, "")
If Not theFolder Is Nothing Then
mypath = theFolder.Items.Item.Path
End If
‘//////////////搜索开始////////////////
nm = Dir(mypath & "\*.*") ‘第一次使用dir,必须指定pathname参数,返回符合条件的第1个文件名
i = 1
Range("a1") = nm ‘单元格A1返回找到的第一个文件名
Do While nm <> ""
nm = Dir ‘再次调用不需要pathname参数
Range("a" & i + 1) = nm
i = i + 1
Loop
Application.ScreenUpdating = True
End Sub
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
GHOST系统的时候出错的原因和解决方法
iOS 14哪个版本最好用?iOS 14.7.1值得升级吗?
![](https://img.aspxhome.com/file/2023/4/45774_0s.png)
Excel中进行带有函数数值复制粘贴的操作技巧
电脑版wps文档如何拆分表格中的单元格
![](https://img.aspxhome.com/file/2023/9/a183519_0s.png)
Win10任务栏不显示年月怎么回事?Win10任务栏不显示年月的解决方法
![](https://img.aspxhome.com/file/2023/1/a279849_0s.jpg)
iOS11.4.1beta5更新了什么内容?iOS11.4.1beta5值得升级吗?
![](https://img.aspxhome.com/file/2023/5/a200025_0s.jpg)
电脑屏幕中间、角落漏白光怎么办?
![](https://img.aspxhome.com/file/2023/6/a321677_0s.jpg)
Win7系统电脑运行软件提示快捷方式存在问题的解决方法
WPS 怎么在PPT中制作缺角文字
![](https://img.aspxhome.com/file/2023/9/a186689_0s.jpg)
wps如何设置大纲级别自动生成文字目录
如何替换PS模板中的图层图片?
![](https://img.aspxhome.com/file/2023/9/a341829_0s.png)
Win10如何关闭用户账户控制提示?Win10关闭用户账户控制提示的方法
![](https://img.aspxhome.com/file/2023/25/a217825_0s.png)
网易有爱插件怎么设置?网易有爱插件设置方法步骤
![](https://img.aspxhome.com/file/2023/7/a328768_0s.jpg)
Win7系统没有找到msvcr100.dll怎么办?
![](https://img.aspxhome.com/file/2023/27/a231136_0s.jpg)
Win7电脑的内存不能为written怎么办?
![](https://img.aspxhome.com/file/2023/26/a228171_0s.png)
xbox控制台点登录没反应怎么回事?xbox控制台无法登陆解决办法
![](https://img.aspxhome.com/file/2023/30/a268455_0s.jpg)
Word如何设置双行合一
![](https://img.aspxhome.com/file/2023/0/34910_0s.jpg)
巧影和PicsArt如何制作隐分身视频?
如何从youtube下载视频?使用Ummy Video Downloader for Mac只需复制地址就可一键下载
![](https://img.aspxhome.com/file/2023/0/a213450_0s.jpeg)
Excel NORM.S.DIST 函数 使用实例教程
![](https://img.aspxhome.com/file/2023/6/a162906_0s.png)