乔山办公网我们一直在努力
您的位置:乔山办公网 > excel表格制作 > <em>EXCEL</em> VBA Dir 函数-excel dir函数,excel函数怎么用

<em>EXCEL</em> VBA Dir 函数-excel dir函数,excel函数怎么用

作者:乔山办公网日期:

返回目录:excel表格制作


据资料,dir 不能嵌套使用。

解决办法:可以尝试先将结果保存到数组,再遍历数组各项 dir ;或者可以借用 Scripting.Dictionary 与 dir 相结合e79fa5e98193e59b9ee7ad94334起来使用。

提供网络代码供参考:

Public Function ListFile(MuLu As String, Zi As Boolean, Optional LeiXing As String = "")
Dim MyFile As String, ms As String
Dim arr, brr, x
Dim i As Integer
Set d = CreateObject("Scripting.Dictionary")
If Left(MuLu, 1) <> "\" Then MuLu = MuLu & "\"
d.Add MuLu, ""
i = 0
Do While i < d.Count
    brr = d.keys
    MyFile = Dir(brr(i), vbDirectory)
    Do While MyFile <> ""
        If MyFile <> "." And MyFile <> ".." Then
            If (GetAttr(brr(i) & MyFile) And vbDirectory) = vbDirectory Then d.Add (brr(i) & MyFile & "\"), ""
        End If
        MyFile = Dir
    Loop
    If Zi = False Then Exit Do
    i = i + 1
Loop
If LeiXing = "" Then
    ListFile = Application.Transpose(d.keys)
Else
    For Each x In d.keys
        MyFile = Dir(x & LeiXing)
        Do While MyFile <> ""
            ms = ms & x & MyFile & ","
            MyFile = Dir
        Loop
        If Zi = False Then Exit For
    Next
    If ms = "" Then ms = "没有符合要求的文件,"
    ListFile = Application.Transpose(Split(ms, ","))
End If
End Function


测试函数:

Public Sub a()
Dim a
a = ListFile("F:\VBA\pdf\Excel2007VBA", True, "*.xls")
Range("a1").Resize(UBound(a), 1) = a
End Sub


.和..是当前目录和上级目录,代zd修改下:
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
不用VBA也可
sheet12 A2列公式
=IF(COUNTIF(Sheet1!$A:$A,$A2)>=0,A2,"")
向下向右复制。
然后对sheet2筛选不为空的,复制后粘贴到新表即可

多截点图,就这几句看不出来问题再哪儿.

相关阅读

关键词不能为空
极力推荐

ppt怎么做_excel表格制作_office365_word文档_365办公网