VBA自动过滤器未按我的标准过滤

时间:2014-09-09 18:38:41

标签: excel vba excel-vba autofilter

我创建了一个之前正在运行的过滤器功能,但是我离开了它几天(周末)并且它没有正确过滤。

我已将下面的代码包含在内进行检查。我已经运行了手表,一切似乎都正确地进入自动过滤器线。我的想法已经不多了。

我可能在纸张本身上发生了什么变化,打破了自动过滤器?我可以在其中一个破坏它的标准中添加一些东西吗?

由于

注意:任何不是DIMed的变量都是全局变量或垃圾变量。此外,sortfield的索引可能已关闭;我正在努力遵循这一点。似乎在正确的列中过滤时,它只是过滤掉所有内容。

Private Sub Filter_CommandButton_Click()
    'Filters by a text input and a column to filter
    Dim isInCol As Boolean
    Dim sortKey As String
    Dim sortOrder As XlSortOrder
    Dim currentData As Range
    Dim sortField As Integer ' This is the offset, determined by list index
    Dim lastRow As Integer ' used to set working range


    If filterColumn Is Nothing Then
        gojira = MsgBox("You need to enter a column to filter", vbOKOnly)
        Exit Sub
    End If

    ' Set target = ActiveWorkbook.Worksheets("Testing_Filter_Form")
    lastRow = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row + 1 ' target.Cells(Rows.Count, "A").End(xlUp).Row + 1

    ' put borders around whole row
    ' target.Range(target.Cells(lastRow, 1), target.Cells(lastRow, 56)).Borders.LineStyle = xlContinuous

    ' setting active sheet complete data
    Set currentData = ActiveSheet.Range(ActiveSheet.Cells(1, 1), ActiveSheet.Cells(lastRow, 12))

    ' make sort key a wildcard
    If include = True Then
        sortKey = "*" & filterText & "*"
    Else ' for excluding the entered text
        sortKey = "<>" & "*" & filterText & "*"
    End If

    ' find list index for sorting
    sortField = Me.ColumnFilter_ComboBox.ListIndex ' + 1

    ' populate sort order with value of Ascend/Descend options.
    If descend Then
        sortOrder = xlDescending
    Else
        sortOrder = xlAscending
    End If

    ' Search column to see if text exists
    isInCol = False
    For Each foo In filterColumn
        If caseSense Then
            If InStr(foo.Value, filterText) Then ' > 0 Then
                isInCol = True
                Exit For
            End If
        ElseIf Not caseSense Then
            If InStr(LCase(foo.Value), LCase(filterText)) Then
                isInCol = True
                Exit For
            End If
        End If
    Next foo

    If isInCol Then ' filter the table by the selected col; Use Range Sort
        With target
            currentData.AutoFilter Field:=sortField, Criteria1:=sortKey, VisibleDropDown:=False
            currentData.Sort key1:=filterColumn, Order1:=sortOrder, Header:=xlYes
        End With
    End If

    blah = bleh
End Sub

0 个答案:

没有答案