EXCEL VBA Dir 函数

我想用 Dir 函数把指定路径里的所有EXCEL 表格的名字提取出来,但我发现多了两个点(下图单元格A4 \ A5),请教一下是什么情况...
Dim spath As String, patha As String
Dim sfilename As String
Dim rown As Long

spath = Cells(2, 1).Value & Application.PathSeparator
sfilename = Dir(spath & "*", vbDirectory)
rown = 4
Do While sfilename <> ""
Cells(rown, 1).Value = sfilename
sfilename = Dir
rown = rown + 1
Loop

这是文件系统发回给DIr的,一个点代表的是本级目录,两个点代表的是上级目录。他们以“文件”的方式保存在这个子文件夹的文件表里面用以保存自身的存放位置和与上级目录的关联
你需要自行鉴别获得的数据(包括可能的子文件夹)追问

额...楼上的速度更快一点...但仍然谢谢你的回答,谢谢!

追答

没事,明白的原因,能解决问题就好。祝你顺利

温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2017-09-06
.和..是当前目录和上级目录,代码修改下:
Dim spath As String, patha As String
Dim sfilename As String
Dim rown As Long

spath = Cells(2, 1).Value & Application.PathSeparator
sfilename = Dir(spath & "*", vbDirectory)
rown = 4
Do While sfilename <> ""
if sfilename<>"." and sfilename<>".." then
Cells(rown, 1).Value = sfilename
end if
sfilename = Dir
rown = rown + 1
Loop追问

原来是这样,dir 函数好像不能识别文件的类型,那有没有一种函数可以识别呢,比如说,在某个文件夹里,我只查找循环EXCEL表格,其它的文件不理?

追答

当然可以选文件类型了:dir("c:\*.xls"),类似这样

本回答被提问者采纳

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网