gpt4 book ai didi

vba - Excel 2007 中的 Application.filesearch 带循环

转载 作者:行者123 更新时间:2023-12-03 02:08:18 27 4
gpt4 key购买 nike

我已经进行了多次搜索,但在使用现在不存在的 .filesearch 时很难找到正确的代码 - 我已经研究过使用 Dir 和 FileSystemObject,但在使用循环时却没有任何困惑搜索后...我希望您能够帮助我得出更简单的结论!

简而言之,我当前的代码在一个文件夹中搜索所有 Excel 文件,然后打开第一个文件,执行所需的操作,关闭它,然后打开下一个搜索到的文件。提前致谢!

FilePath = "S:\My\File\Path"
FileSpec = ".xls"

Set FS = Application.FileSearch
With FS
.LookIn = FilePath
.Filename = FileSpec
.Execute
End With

For b = 1 To FS.FoundFiles.Count
StrFile = FS.FoundFiles(b)

Set mobjXL = New Excel.Application
With mobjXL

.Visible = False

'REST OF CODE HERE

next b

最佳答案

这应该会让你指向正确的方向:

Sub Your_Sub()

Dim FSO as Object
Dim FSO_FOLDER AS Object
Dim FSO_FILE as Object
Dim FILE_PATH as String
Dim FILE_EXT as String

FILE_PATH = "S:\My\File\Path"
FILE_EXT = "xls"

''Create FileSystem Objects
Set FSO = CreateObject("Scripting.FileSystemObject")
Set FSO_FOLDER = FSO.GetFolder(FILE_PATH)

If FSO_FOLDER.Files.Count > 0 Then

''Loop through each File in Folder
For Each FSO_FILE IN FSO_FOLDER.Files

''Test extension
If FSO.GetExtensionName(FSO_FILE.Name) = FILE_EXT Then
''Do your thing here
Else:End if

Next

Else

Msgbox "No Files Found at " & FILE_PATH

End If

Set FSO = Nothing
Set FSO_FOLDER = Nothing

End Sub

关于vba - Excel 2007 中的 Application.filesearch 带循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17671725/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com