VBA使用过滤器来排除字符串数组中的项

时间:2014-05-12 08:13:54

标签: vba excel-vba excel

首先,如果下面的代码不包含数组而是列表,请原谅。我是VBA的新手,所以我还不知道所有的基础知识。下面的代码检索某个文件夹中所有Excel文件的数组(或列表)。该代码用于从每个文件中检索数据并粘贴在主文件中 好的,这是我的MWE:

    Sub ReadDataFromAllWorkbooksInFolder()
    Dim FolderName As String, wbName As String, wbCount As Integer
    FolderName = "C:\Users\Robin\Desktop\Test"
        wbCount = 0
        wbName = Dir(FolderName & "\" & "*.xlsm")
        While wbName <> ""
            wbCount = wbCount + 1
            ReDim Preserve wbList(1 To wbCount)
            wbList(wbCount) = wbName
            wbName = Dir
        Wend
        If wbCount = 0 Then Exit Sub
    End Sub

现在出现问题:数组包含模板文件,文件名包含单词 TEMPLATE 。如何过滤(或排除)文件名包含字符串&#34; TEMPLATE&#34;?

任何帮助将不胜感激!提前谢谢!

3 个答案:

答案 0 :(得分:2)

变量wbName包含Dir命令的文件名。检查字符串是否包含您要排除的内容。

延长线

While wbName <> ""

While wbName <> "" And InStr(1, UCase(wbName), "TEMPLATE") = 0

排除包含TEMPLATE的所有文件名,无论如何。

修改

上面我的建议中的逻辑是错误的,它在遇到包含“TEMPLATE”的第一个文件名时停止了。请尝试以下方法:

    While wbName <> ""
        '***** Only add to wbCount if wbName does not contain TEMPLATE
        If InStr(1, UCase(wbName), "TEMPLATE") = 0 Then
            wbCount = wbCount + 1
            ReDim Preserve wbList(1 To wbCount)
            wbList(wbCount) = wbName
        End If
        wbName = Dir
    Wend

编辑2

有关查找错误的一些常规提示:

  • 始终在模块顶部使用Option Explicit。它会强制你声明你的变量,最大限度地减少错别字引入错误的风险。
  • 使用 F8 逐步执行代码以识别错误并检查变量和函数的变量
  • 如果可能,删除代码以简化它,只保留While循环和If语句之类的逻辑,以确保基础工作正常。然后添加详细信息。

最后,我的解决方案的关键是找到名称中包含“TEMPLATE”的文件。对于包含字符串“TEMPLATE”的所有文件,语句InStr(1, UCase(wbName), "TEMPLATE") = 0将为False,无论大小写如何,否则为True。 (Ucase部分是使语句不区分大小写的原因。)

尝试将其添加到您的逻辑中。

答案 1 :(得分:2)

Dim fNameList As Variant
Dim storFName As String
Dim i As Integer
Sub ReadDataFromAllWorkbooksInFolder()
Dim FolderName As String, wbName As String, wbCount As Integer
FolderName = "C:\Users\Robin\Desktop\Test"
    wbCount = 0
    wbName = Dir(FolderName & "\" & "*.xlsm")
    While wbName <> ""
        wbCount = wbCount + 1
        ReDim Preserve wbList(1 To wbCount)
        fNameList = Split(wbName, ".")
        For i = 0 To UBound(fNameList) '<- Here i is being use
            If (UCase(InStr(fNameList(i), "TEMPLATE"))) Then
               UCase(storFName) = fNameList (i)
            End If
        Next i '<- Here i is being increase
        If storFName <> "TEMPLATE" Then
            wbList(wbCount) = wbName
            wbName = Dir
        End If
    Wend
    If wbCount = 0 Then Exit Sub
End Sub

它会对你有用吗?

答案 2 :(得分:0)

Sub test()
    Dim x() As String, i As Integer
    x = ReadDataFromAllWorkbooksInFolder("C:\Users\Robin\Desktop\Test")
    For i = 0 To UBound(x)
        Debug.Print x(i)
    Next i
End Sub

Private Function ReadDataFromAllWorkbooksInFolder(FolderName As String) As Variant
    Dim wbName As String
    Dim wbCount As Integer
    Dim wbList() As String
    wbCount = 0
    ReDim wbList(0)
    wbName = Dir(FolderName & "\*.xlsm")
    While wbName <> vbNullString
        If Not (UCase(wbName) Like "*TEMPLATE*") Then
            ReDim Preserve wbList(wbCount)
            wbList(wbCount) = wbName
            wbCount = wbCount + 1
        End If
        wbName = Dir
    Wend
    ReadDataFromAllWorkbooksInFolder = wbList
End Function

一旦你对VBA感到满意,你可能会转而使用Scripting.FileSystemObject和Scripting.Dictionary ......