首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用FSO搜索列范围中的Excel文件

使用FSO搜索列范围中的Excel文件
EN

Stack Overflow用户
提问于 2014-09-15 09:34:44
回答 2查看 331关注 0票数 0

在下面的代码中,我试图实现的是,代码搜索在给定路径中的列范围F中输入的文件,即“D:\Checksheets”。我仍然在学习FSO,并将非常感谢任何帮助。

代码语言:javascript
复制
Sub Test()

Dim FSO As Object
Dim FSO_Folder As Object
Dim FSO_file As Object

Dim path As String


Dim sheetref As String
Dim nextform As String
Dim row As Integer
Dim col As Integer

row = 8
col = 6

sheetref = Sheets("Sheet1").Cells(row, col)

'nextform = sheetref

path = "D:\Checksheets\"

Do Until Sheets("Sheet1").Cells(row, col) = "END"

    Set FSO = CreateObject("Scripting.FileSystemObject")
    Set FSO_Folder = FSO.GetFolder(path)

    For Each FSO_file In FSO_Folder.Files
        If FSO_file.Name = sheetref Then

        MsgBox "done" & path
    Else

    End If
    row = row + 1
    Next

Loop

End Sub
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-09-15 10:44:52

多亏了亚历克斯,我才能让代码正常工作。如果有人有类似的问题,下面是代码:

代码语言:javascript
复制
Sub test()
Set FSO = CreateObject("Scripting.FileSystemObject")

Dim sht As Worksheet, cell As Range
Dim row As Integer
Dim col As Integer
Dim path As String

path = "D:\Checksheets\"

row = 1
col = 6
Set sht = Sheets("Sheet1")

Do

    Set cell = sht.Cells(row, col)

    If cell.Value = "END" Then Exit Do

    If cell.Value <> "" Then    ' checks for any empty cells

    FSO.FileExists (path)

        MsgBox "file exists"

    Else

    End If



    row = row + 1
Loop

End Sub
票数 0
EN

Stack Overflow用户

发布于 2014-09-15 09:56:04

FSO有一个内置的FileExists方法:

代码语言:javascript
复制
...
Set FSO = CreateObject("Scripting.FileSystemObject")

Dim sht As Worksheet, cell As Range
Set sht = Sheets("Sheet1")

Do
    Set cell = sht.Cells(row, col)

    If cell.Value = "END" Then Exit Do

    If FSO.FileExists(path & cell.Value) Then
        MsgBox "done " & cell.Value
    End If

    row = row + 1
Loop

您可以完全删除FSO代码,并用内置的FileExists函数替换Dir$调用:

代码语言:javascript
复制
If Len(Dir$(path & cell.Value)) Then
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25844925

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档