先上图,看看是你想要的结果么
你写的内容有点歧义,我理解成有好多以身份证命名的文件在不同的子文件夹下面,你想列出他们分别都在在哪个文件夹里是吧。
给你代码
Sub aaa()
Dim k, p, q, i, m As Integer
Dim path_str(1 To 100), ext(1 To 50), path_0 As String '如果大于100个子文件夹 请修改path_str内的参数
'定义总文件夹
path_0 = "d:\身份\"
'定义文件拓展名,如果有其他类型的文件,自己再接着添加 ext(*) = "*" 如果没有添加该类型文件,就找不到了。注意前面要有那个点
ext(1) = ".xls"
ext(2) = ".xlsx"
ext(3) = ".doc"
ext(4) = ".txt"
For p = 1 To 50
If ext(p) = "" Then Exit For
Next
Myfile = Dir(path_0, vbDirectory)
Do While Myfile <> ""
If GetAttr(path_0 & Myfile) = vbDirectory Then
k = k + 1
path_str(k) = Myfile
End If
Myfile = Dir
Loop
For i = 1 To Range("a60000").End(xlUp).Row ‘to后面可以自动判断你这个表的最后一行。如果你想手工改动要判断几行,可以自己把Range("a60000").End(xlUp).Row 替换成一个数字
For m = 1 To k
path_1 = path_0 & path_str(m) & "\"
For q = 1 To p - 1
If CreateObject("Scripting.FileSystemObject").FileExists(path_1 & Cells(i, 1).Value & ext(q)) Then Cells(i, 2).Value = path_str(m)
Next
Next
Next
End Sub