自动过滤器错误(查看错误的列)

时间:2016-05-06 13:37:53

标签: excel-vba vba excel

我使用if语句和.autofilter来搜索不同的列,具体取决于我读取的文本文件的前两位数字。我的问题是它在文本文件中搜索相应列的第一行数据,然后每次在循环中运行时搜索相同的列。这就是我所拥有的:

Dim myFile As String

myFile = Application.GetOpenFilename()

Open myFile For Input As #1

Dim source As String
Dim destination As Range

Dim rowNum As Long

Set destination = Sheets("SHOULDER").Range("A2")

Do Until EOF(1)

    Line Input #1, source

    If Mid(source, 1, 2) = "09" Then

        With destination.Columns("B:C")
            .AutoFilter field:=1, Criteria1:=Mid(source, 3, 4)
            .AutoFilter field:=2, Criteria1:=Mid(source, 7, 4)
        End With

        rowNum = Range("A" & Rows.Count).End(xlUp).row
        Cells(rowNum, 8).Value = Mid(source, 23, 2) & " " & Mid(source, 21, 2) & " " & Mid(source, 19, 2)
        Cells(rowNum, 9).Value = Mid(source, 11, 2) & "/" & Mid(source, 13, 2) & "/" & Mid(source, 15, 4)

    ElseIf Mid(source, 1, 2) = "15" Then

        With destination.Columns("L:M")
            .AutoFilter field:=1, Criteria1:=Mid(source, 3, 4)
            .AutoFilter field:=2, Criteria1:=Mid(source, 7, 4)
        End With

        rowNum = Range("A" & Rows.Count).End(xlUp).row
        Cells(rowNum, 18).Value = Mid(source, 23, 2) & " " & Mid(source, 21, 2) & " " & Mid(source, 19, 2)
        Cells(rowNum, 19).Value = Mid(source, 11, 2) & "/" & Mid(source, 13, 2) & "/" & Mid(source, 15, 4)

    ElseIf Mid(source, 1, 2) = "23" Then

        With destination.Columns("V:W")
            .AutoFilter field:=1, Criteria1:=Mid(source, 3, 4)
            .AutoFilter field:=2, Criteria1:=Mid(source, 7, 4)
        End With

        rowNum = Range("A" & Rows.Count).End(xlUp).row
        Cells(rowNum, 28).Value = Mid(source, 23, 2) & " " & Mid(source, 21, 2) & " " & Mid(source, 19, 2)
        Cells(rowNum, 29).Value = Mid(source, 11, 2) & "/" & Mid(source, 13, 2) & "/" & Mid(source, 15, 4)

    End If
Loop
    Selection.AutoFilter

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

您需要先删除已应用的Range.AutoFilter Method,然后再循环到读取 source 的下一行。您正在取消已应用的.AutoFilter 外部循环,而不是循环内部。

With Worksheets("SHOULDER")
    With .Range(.Cells(2, "A"), .Cells(Rows.Count, "W").End(xlUp))
        Do Until EOF(1)
            Line Input #1, Source
            Select Case Mid(Source, 1, 2)
                Case "09"
                    .AutoFilter field:=2, Criteria1:=Mid(Source, 3, 4)
                    .AutoFilter field:=3, Criteria1:=Mid(Source, 7, 4)
                Case "15"
                    .AutoFilter field:=12, Criteria1:=Mid(Source, 3, 4)
                    .AutoFilter field:=13, Criteria1:=Mid(Source, 7, 4)
                Case "23"
                    .AutoFilter field:=22, Criteria1:=Mid(Source, 3, 4)
                    .AutoFilter field:=23, Criteria1:=Mid(Source, 7, 4)
                Case Else
                    'do nothing
            End Select

            rowNum = .Range("A" & Rows.Count).End(xlUp).Row
            .Cells(rowNum, 28).Value = Mid(Source, 23, 2) & " " & Mid(Source, 21, 2) & " " & Mid(Source, 19, 2)
            .Cells(rowNum, 29).Value = Mid(Source, 11, 2) & "/" & Mid(Source, 13, 2) & "/" & Mid(Source, 15, 4)

            'this is what was missing - reset the .AutoFilter in prep for the next one
            If .Parent.AutoFilterMode Then .Parent.AutoFilterMode = False
        Loop
    End With
End With

我已经重写了使用Select Case statement的逻辑并减少了冗余代码。它可能不完全正确,但我希望你能看到我在哪里重置.AutoFilter以接受循环中的下一个。