vba - 当我们使用Excel vba中的Dir函数循环遍历文件夹(文件)时,是否有类似于 'Find'的方法可用?

标签 vba excel vbscript

众所周知,我们使用Find()方法来查找Excel中是否存在字符串或任何Microsoft Excel数据类型

(通常我们在一组数据上执行此操作)

我想知道当我们使用 Dir 函数循环遍历文件夹(文件)时是否有可用的此类方法。

情况:

我有一个 Excel -“FileNames.xlsx”,其中“Sheet1”的文件名称具有扩展名 .pdf/.jpg/.jpeg/.xls/.xlsx/.png./.txt/.docx/.rtf 在 A 列。

我有一个名为“Folder”的文件夹,其中包含“FileNames.xlsx”中的大部分(或全部)文件。

我必须检查“FileNames.xlsx”中提到的所有文件名是否都存在于“文件夹”中。

为此,我编写了以下 VBScript(.vbs):

strMessage =Inputbox("Enter No. of Files in Folder","Input Required")  
set xlinput = createobject("excel.application")
set wb123 =xlinput.workbooks.Open("E:\FileNames.xlsx")

set sh1 =wb123.worksheets("Sheet1")

    For i = 2 to strMessage +1

    namei = sh1.cells(i,1).value

    yesi = "E:\Folder"+ namei +

    If namei <> yesi Then
        sh1.cells(i,1).Interior.Color = vbRed
    Else
    End If
    Next

    msgbox "Success"
xlinput.quit

由于我无法获得所需的输出,我尝试记录一个小的 Excel VBA 宏。 (将 FileNames.xlsx 更改为 FileNames.xlsm)

Sub LoopThroughFiles()

    Dim lastRow As Long

    lastRow = Sheets("Sheet1").UsedRange.Rows.Count

    Dim MyFolder As String
    Dim filename As Range
    Dim MyFile As String

    MyFolder = "E:\Folder"
    For Each filename In Worksheets("Sheet1").Range("A2A:" & lastRow)

    MyFile = Dir(MyFolder & "\*.xlsx")
    'Here I actually need to pass all file extensions to Dir

    Do While MyFile <> ""
    If filename = MyFile Then
    'Do Nothing
    Else
    filename.Interior.Color = vbRed

    MyFile = Dir

    Next
End Sub

以上是一次失败的尝试。

我想尝试一下类似于Find()的方法

Sub LoopThroughFiles()
    Dim lastRow As Long
    'Dim LastFile As Long  
    'Is there need of it (LastFile variable)? I kept this variable 
    'to save (prior known) count of files in folder.

    lastRow = Sheets("Sheet1").UsedRange.Rows.Count
    'LastFile = 'Pass count of Files in folder to this variable.

    Dim fileName As Range
    For Each fileName In Worksheets("Sheet1").Range("A2:A" & lastRow)

    Dim rngFnder As Range

    On Error Resume Next

    'Error at below line. 
     Set rngFnder = Dir("E:\Folder\").Find(filename)    
    'This line gives me error 'Invalid Qualifier' 
    'I am trying to use method similar to Find()     

        If rngFnder Is Nothing Then
        filename.Interior.Color = vbRed
        End If
    Next
End Sub

但是,我无法达到结果。谁能告诉我,在使用 Dir 循环文件夹后,是否有任何此类函数可用于“查找”excel 中的所有文件名是否存在于文件夹中?

据我所知,Dir 函数一次仅适用于一个文件扩展名。 是否可以同时对多个文件扩展名使用 Dir 函数?

预期输出:

假设“FileNames(.xlsx/.xlsm)”中有 8 个文件名。其中“文件夹”中找不到 Arabella.pdfClover.png,然后我想在 Excel 中为这些文件名的单元格着色为红色背景,如下图所示.

Dir and Find() with multiple file extensions

最佳答案

Sub LoopThroughFiles()

    Dim lastRow As Long
    lastRow = Sheets("Sheet1").UsedRange.Rows.Count

    Dim MyFolder As String
    Dim filename As Range
    Dim MyFile As String

    MyFolder = "E:\Folder"
    For Each filename In Worksheets("Sheet1").Range("A2:A" & lastRow)

        MyFile = MyFolder & "\" & filename
        If Not FileExists(MyFile) Then
            filename.Interior.Color = vbRed
        End If

    Next
End Sub

Public Function FileExists(strFullpathName As String) As Boolean

If Dir(strFullpathName) <> "" Then
    FileExists = True
Else
    FileExists = False
End If

End Function

关于vba - 当我们使用Excel vba中的Dir函数循环遍历文件夹(文件)时,是否有类似于 'Find'的方法可用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31874429/

相关文章:

excel - 连接日期和小数时间,格式为 mm/dd/yyyy hh :mm format

javascript - Onclick命令不会将ID发送到文本框,onclick需要 "escaping"才能被识别为代码

vba - Excel VBA 查找并替换数字前的所有字符

excel - 如何在Excel中创建动态按钮

vba - 从PPT中提取文本并使用VBA将其粘贴到Excel中

c# - Excel 到 pdf,图像质量差?

VBA 交货收据请求 Excel

excel - 如何复制/选择工作表中用边框包围的单元格范围?

html - 使用 VBScript 捕获 HTA 窗口关闭事件

vba - VBScript 打开一个对话框来选择文件路径