Application.filesearch在Excel 2007 w /循环 [英] Application.filesearch in Excel 2007 w/loop

查看:407
本文介绍了Application.filesearch在Excel 2007 w /循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经做了几次搜索,当我使用现在不存在的.filesearch时我找不到正确的代码 - 我已经研究了使用Dir和FileSystemObject,但是在使用循环时并没有什么困惑在搜索之后...我希望你可以帮助我更容易得出结论!



简而言之,我目前的代码搜索所有excel的文件夹文件,并打开第一个,做它需要做的,关闭它,并打开下一个搜索的文件。感谢提前!

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

设置FS = Application.FileSearch
与FS
.LookIn = FilePath
.Filename = FileSpec
.Execute
结束

对于b = 1到FS.FoundFiles.Count
StrFile = FS.FoundFiles(b)

设置mobjXL =新的Excel.Application
使用mobjXL

.Visible = False

'REST OF CODE HERE

next b
pre>

解决方案

这应该让您指向正确的方向:

  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

''创建FileSystem对象
设置FSO = CreateObject(Scripting.FileSystemObject)
设置FS O_FOLDER = FSO.GetFolder(FILE_PATH)

如果FSO_FOLDER.Files.Count> 0然后

''循环通过文件夹中的每个文件
每个FSO_FILE IN FSO_FOLDER.Files

''测试扩展
如果FSO.GetExtensionName( FSO_FILE.Name)= FILE_EXT然后
''你的东西在这里
否则:如果

下一个

Else

Msgbox找不到文件& FILE_PATH

如果

设置FSO =没有
设置FSO_FOLDER =没有

结束Sub


I've done several searches and I'm having trouble to find the right code when using the now non-existent .filesearch - I've looked into using both Dir and FileSystemObject but with nothing short of confusion when using a loop after the search... I'm hoping you might be able to help me come to an easier conclusion!

In short, my current code searches a folder for all excel files, and opens the first, does what it needs to do with it, closes it, and opens the next searched file. Thanks in advance!

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

解决方案

This should get you pointed in the right direction:

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

这篇关于Application.filesearch在Excel 2007 w /循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆