能否用excel搜索一个文件夹里是否有某个名字的txt文本文件?如果有怎么弄?
能否用excel搜索一个文件夹里是否有某个名字的txt文本文件?如果有怎么弄?目的是检查这个名字文件夹下是否有这个名字的文件,还有就是可以连续检查。最好是第一列是我输入的...
能否用excel搜索一个文件夹里是否有某个名字的txt文本文件?如果有怎么弄?目的是检查这个名字文件夹下是否有这个名字的文件,还有就是可以连续检查。最好是第一列是我输入的名,第二列就能检查跳出这个名字。
展开
2个回答
展开全部
答:可以实现,通过编程完成。
比如:D:\示例文件夹里有如下文件
工作表里有清单:
运行程序后得到:
按ALT+F11组合键,打开VB窗口,执行"插入"-"模块",复制下面代码进去,按F5运行“Demo"程序即得到结果。
Dim FindedNames() As String
Dim NumNames As Long
Sub Demo()
Dim FilePath As String
Dim FileName As String
Dim Cell As Range
Set Cell = Range("A2")
Dim Finded As Boolean
FilePath = "D:\示例文件夹\"
FileName = "*.*"
Call ReDir(FilePath, FileName)
Do Until IsEmpty(Cell)
Finded = False
For i = 0 To UBound(FindedNames)
If Cell = Split(FindedNames(i), ".")(0) Then Finded = True: Exit For
Next
If Finded Then
Cell.Offset(0, 1) = "找到"
Else
Cell.Offset(0, 1) = "没找到"
End If
Set Cell = Cell.Offset(1, 0)
Loop
End Sub
Public Sub ReDir(ByVal CurrDir As String, ByVal FindName As String)
Dim Dirs() As String
Dim NumDirs As Long
Dim TotalFiles, SingleFile
Dim TotalFolders, SingleFolder
Dim fso As Object
Set fso = CreateObject("scripting.filesystemobject")
Set TotalFiles = fso.GetFolder(CurrDir).Files
Set TotalFolders = fso.GetFolder(CurrDir).SubFolders
If TotalFiles.Count <> 0 Then
For Each SingleFile In TotalFiles
If fso.GetFileName(SingleFile) Like FindName Then
ReDim Preserve FindedNames(0 To NumNames) As String
FindedNames(NumNames) = fso.GetFileName(SingleFile)
NumNames = NumNames + 1
End If
Next
End If
If TotalFolders.Count <> 0 Then
For Each SingleFolder In TotalFolders
ReDim Preserve Dirs(0 To NumDirs) As String
Dirs(NumDirs) = SingleFolder
NumDirs = NumDirs + 1
Next
End If
For i = 0 To NumDirs - 1
Call ReDir(Dirs(i), FindName)
Next i
End Sub
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询