【问题标题】:vba loop through files in folder and copy names if multiple conditions are met/not met如果满足/不满足多个条件,vba 循环遍历文件夹中的文件并复制名称
【发布时间】:2019-02-03 04:31:16
【问题描述】:

我想遍历一个文件夹并复制所有不包含 A6 中的“string1”、B6 中的“string2”、C6 中的“string3”、D6 中的“string4”的 excel 文件的名称。请注意,所有条件都应为真(AND 语句)。 应该测试的单元位于工作表 3 中,称为“ProjectOperation”。

以下代码复制了特定文件夹中所有 excel 的文件名,但是我很难实现条件。请帮忙。

Option Explicit

Sub SubDirList() 'Excel VBA process to loop through directories listing files
Dim sname As Variant
Dim sfil(1 To 1) As String
sfil(1) = "C:\Users\test" 'Change this path to suit.

For Each sname In sfil()
SelectFiles sname
Next sname

End Sub

Private Sub SelectFiles(sPath) 'Excel VBA to show file path name.
Dim Folder As Object
Dim file As Object
Dim fldr
Dim oFSO As Object
Dim i As Integer

'For Each file In Folder
 '       If checknameExistens(Folder.Files) Then

Set oFSO = CreateObject("Scripting.FileSystemObject")
Set Folder = oFSO.GetFolder(sPath)
i = 1
For Each fldr In Folder.SubFolders
SelectFiles fldr.Path
Next fldr

For Each file In Folder.Files
'If checknameExistens(Folder.Files) Then
Range("A6536").End(xlUp)(2).Value = file
i = i + 1
Next file

Set oFSO = Nothing
End Sub

原代码来自以下链接:http://www.thesmallman.com/list-files-in-subdirectory/

【问题讨论】:

  • 你有没有尝试过?这些条件是 AND 还是 OR?
  • 你说的是哪张,总是第一张?
  • 是的,我尝试了不同的东西,最新的是注释掉的部分'对于文件夹中的每个文件'如果 checknameExistens(Folder.Files) 那么但它不起作用,所以我删除了它。条件是 AND,它应该是名为“ProjectOperation”的工作表 3
  • 那么我建议编辑帖子并添加附加信息。你怎么知道它总是第 3 页?
  • 好的,我会这样做的。这是一个很好的问题 - 它是第 3 表或名为“ProjectOperation”的表

标签: vba excel loops directory conditional-statements


【解决方案1】:

首先我更改了检索文件的代码,因为它收集所有文件,无论它是否是 excel 文件。我还将它更改为一个函数,将所有文件返回到一个集合中

Function SelectFiles(ByVal sPath As String, ByVal pattern As String) As Collection

Dim Folder As Object
Dim file As Object
Dim fldr
Dim oFSO As Object
Dim coll As New Collection

    Set oFSO = CreateObject("Scripting.FileSystemObject")
    Set Folder = oFSO.GetFolder(sPath)

    For Each fldr In Folder.SubFolders
        SelectFiles fldr.path, pattern
    Next fldr

    For Each file In Folder.Files
        If file.Name Like pattern Then
            coll.Add file
        End If
    Next file

    Set SelectFiles = coll

End Function

然后我使用以下函数检索您可以找到here resp 的文件的内容。 here

Private Function GetValue(path, file, sheet, ref)
'   Retrieves a value from a closed workbook
Dim arg As String
    '   Make sure the file exists
    If Right(path, 1) <> "\" Then path = path & "\"
    If Dir(path & file) = "" Then
        GetValue = "File Not Found"
        Exit Function
    End If
    '   Create the argument
    arg = "'" & path & "[" & file & "]" & sheet & "'!" & _
          Range(ref).Range("A1").Address(, , xlR1C1)
    '   Execute an XLM macro
    GetValue = ExecuteExcel4Macro(arg)
    If IsError(GetValue) Then GetValue = ""

End Function

这是最终的结果

Sub TestList()
Const SH_NAME = "ProjectOperation"
Dim sname As Variant
Dim coll As Collection
Dim s1 As String
Dim s2 As String
Dim s3 As String
Dim s4 As String
Dim i As Long

    sname = "...."     'Change this path to suit.

    Set coll = SelectFiles(sname, "*.xls*")

    For i = 1 To coll.Count
        s1 = GetValue(coll.Item(i).parentfolder, coll.Item(i).Name, SH_NAME, "A6")
        s2 = GetValue(coll.Item(i).parentfolder, coll.Item(i).Name, SH_NAME, "B6")
        s3 = GetValue(coll.Item(i).parentfolder, coll.Item(i).Name, SH_NAME, "C6")
        s4 = GetValue(coll.Item(i).parentfolder, coll.Item(i).Name, SH_NAME, "D6")
        If s1 = "string1" And s2 = "string2" And s3 = "string3" And s4 = "string4" Then
            Debug.Print coll.Item(i).path
        End If
    Next

End Sub

【讨论】:

  • 首先,非常感谢您的回复! :) 但是我无法让代码工作。也许我没有正确解释我的问题,或者只是不理解代码。我将在一个示例中进行解释:我有 3 个名为 e1、e2 和 e3 的 excel 文档。 e1 和 e2 相同(它们在 A6、B6、C6、D6 中具有相同的文本)但 e3 在单元格 D6 中具有不同的文本。因此,我希望将 e3 的工作簿名称列在我的 VBA excel 中,因为它不符合其他条件。感谢您的时间! :)
  • 我建议编辑您的帖子以使您的问题更清楚,因为根据您的评论,这似乎与我的理解不同。无论如何,我认为,您也可以使用我的部分代码来做您想做的事情,因为SelectFiles 为您提供了所有 excel 文件的集合,GetValue 为您提供了 excel 文件中某些单元格的值。如果您根据需要将其组合起来,它应该可以工作。你只需要适应TestList 应该没那么难。
【解决方案2】:

我使用了您现有的代码,并且刚刚在您的循环中添加了一条 If 语句(以及一些新变量的声明)。因为您现在正在处理两个文件,所以您需要在引用范围时正确引用工作簿和工作表。

'...
Dim wb As Workbook, ws As Worksheet

Application.ScreenUpdating = False
For Each file In Folder.Files
    Set wb = Workbooks.Open(file)
    Set ws = wb.Sheets("ProjectOperation")
    If ws.Range("A6").Value = "string1" And ws.Range("B6").Value = "string2" And _
       ws.Range("c6").Value = "string3" And ws.Range("D6").Value = "string4" Then
        ThisWorkbook.Sheets(1).Range("A" & Rows.Count).End(xlUp)(2).Value = file 'workbook/sheet references may need changing
        i = i + 1
    End If
    wb.Close False
Next file
Application.ScreenUpdating = True
'...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-14
    相关资源
    最近更新 更多