简体   繁体   English

.ShowAllData 高级筛选后,表未完全“清除”

[英].ShowAllData after Advanced Filter, Table not fully "clearing"

I've got an issue with a search function I'm building.我正在构建的搜索 function 出现问题。

The actual filter seems to work pretty well and returns what is expected.实际的过滤器似乎工作得很好并且返回了预期的结果。 The user selects criteria from several drop down lists, those are then written to the criteria field that is used in the advanced filter.用户从几个下拉列表中选择条件,然后将这些条件写入高级过滤器中使用的条件字段。

The Problem comes when I go to clear the advanced filter (code below).当我 go 清除高级过滤器(下面的代码)时,问题就来了。 The table does indeed get "cleared", however there is really odd.. Formatting?该表确实被“清除”了,但是真的很奇怪..格式化? afterwards.然后。

nearly all of the rows on the table have the same background (instead of alternating light - dark - light etc), except the rows that had been the results of the previous filter.表格上几乎所有的行都具有相同的背景(而不是交替的亮-暗-亮等),除了那些是上一个过滤器结果的行。

this is causing issues when a new filter is applied to the table, wherein all rows AFTER the last row from the previous filter will not get hidden, and if the table is "cleared" again, only the rows UP UNTIL that last row will show, requiring me to manually unhide those rows at the end of the datatable.当将新过滤器应用于表时,这会导致问题,其中前一个过滤器的最后一行之后的所有行都不会被隐藏,并且如果再次“清除”表,则只有最后一行 UP UNTIL 的行会显示,要求我手动取消隐藏数据表末尾的那些行。

The weirdness does correct itself after double clicking into a cell to edit and then clicking out of it.在双击单元格进行编辑然后单击退出后,这种怪异现象确实会自行纠正。 This isn't a feasible fix however and I'm not even sure how to code something like that in...然而,这不是一个可行的修复方法,我什至不确定如何编写类似的代码......

I know that applying a filter over a filter can create weirdness but this is happening even when I run things manually line by line.我知道在过滤器上应用过滤器会产生怪异,但即使我逐行手动运行时也会发生这种情况。

I'm honestly not sure what I'm doing wrong here or what's happening with the code so if anyone has any insight I'd be grateful!老实说,我不确定我在这里做错了什么或代码发生了什么,所以如果有人有任何见解,我将不胜感激!

Public Sub Apply_Filters(Optional button_name As String)

Const ProcName As String = "Apply_Filters"
On Error GoTo Whoa

Dim WsCP As Worksheet: Set WsCP = ActiveWorkbook.Sheets("Core Pack BDDS")
Dim WsDND As Worksheet: Set WsDND = ActiveWorkbook.Sheets("DO NOT DELETE")
Dim WsSizes As Worksheet: Set WsSizes = ActiveWorkbook.Sheets("Sizes DO NOT DELETE")

'Stuff to be able to find specific categories in the BDDS data table
Dim TableHeaders As Variant: TableHeaders = "Table1[#Headers]"                  'Header row for the main data table
Dim MainDataTable As String: MainDataTable = "Table1"                           'Should be the main table on the BDDS
Dim MainTable As ListObject: Set MainTable = WsCP.ListObjects(MainDataTable)    'Mimics synax to call on the main data table as a variable (to make things cleaner)
Dim WholeMainTable As Range: Set WholeMainTable = WsCP.Range(WsCP.Range(TableHeaders), WsCP.Range(TableHeaders).End(xlDown))

Dim Grp1Criteria As Range

Dim StartTime As Double
Dim ElapsedTime As Double

'Dim button_name As String: button_name = "Test"

'StartTime = MicroTimer

    WsSizes.Range("AD10:AP10").Calculate 'ensuring cells are updated before use
    WsSizes.Range("AD14:AJ14").Calculate 'ensuring cells are updated before use
    WsSizes.Range("AD16:AH16").Calculate 'ensuring cells are updated before use

    If WsSizes.Range("AD10").Value = 0 And WsSizes.Range("AD14").Value = 0 And WsSizes.Range("AD16").Value = 0 Then
        Debug.Print button_name & " - " & ProcName & " - " & " Filters NOT applied"
        GoTo SafeExit
            Else
            
            Call Clear_BDDS_Table
            
            WsDND.Range("BZ4:CS4").Calculate 'ensuring cells are updated before use
            
            Set Grp1Criteria = WsDND.Range("BZ3").CurrentRegion
            
            WholeMainTable.AdvancedFilter xlFilterInPlace, Grp1Criteria
            
            Debug.Print button_name & " - " & ProcName & " - " & " Filters applied"
    End If
    
    ElapsedTime = MicroTimer - StartTime
    
SafeExit:
    
    Debug.Print button_name & " - " & ProcName & " - " & ElapsedTime & " seconds"
    
    Exit Sub

Whoa:
    Debug.Print "'" & ProcName & "' Run-time error '" _
        & Err.Number & "':" & vbLf & "    " & Err.Description
    MsgBox "'" & ProcName & "' Run-time error '" _
        & Err.Number & "':" & vbLf & "    " & Err.Description
    Resume SafeExit

End Sub

The problem seems to be coming from when I'm trying to clear the filters.问题似乎出在我试图清除过滤器时。 I use the following code我使用以下代码

Public Sub Clear_BDDS_Table(Optional button_name As String)

Const ProcName As String = "Clear_BDDS_Table"
On Error GoTo Whoa

Dim WsCP As Worksheet: Set WsCP = Sheets("Core Pack BDDS")
Dim TableHeaders As Variant: TableHeaders = "Table1[#Headers]"                  'Header row for the main data table
Dim MainDataTable As String: MainDataTable = "Table1"                           'Should be the main table on the BDDS
Dim MainTable As ListObject: Set MainTable = WsCP.ListObjects(MainDataTable)    'Mimics synax to call on the main data table as a variable (to make things cleaner)
Dim WholeMainTable As Range: Set WholeMainTable = WsCP.Range(WsCP.Range(TableHeaders), WsCP.Range(TableHeaders).End(xlDown))

If WsCP.FilterMode = True Then
    WsCP.ShowAllData
End If

    Debug.Print button_name & " - " & ProcName & " ran successfully"

SafeExit:

    Exit Sub

Whoa:
    Debug.Print button_name & " - " & "'" & ProcName & "' Run-time error '" _
        & Err.Number & "':" & vbLf & "    " & Err.Description
    MsgBox "'" & ProcName & "' Run-time error '" _
        & Err.Number & "':" & vbLf & "    " & Err.Description
    Resume SafeExit

End Sub

I seem to have figured out the problem.我似乎已经想通了这个问题。

This post here was helpful in fixing this problem... autofilter not including all rows when filtering using vba此处的这篇文章有助于解决此问题... 使用 vba 过滤时自动过滤器不包括所有行

Seems to have stemmed from when I was declaring the table range at the beginning of the Apply_Filters sub.似乎源于我在 Apply_Filters 子的开头声明表范围。

VBA stored the last row in that range as the end of the table and after the table was cleared that stayed as the last row. VBA 将该范围内的最后一行存储为表的末尾,并在清除表后保留为最后一行。

Dim TableHeaders As Variant: TableHeaders = "Table1[#Headers]"                  'Header row for the main data table
Dim MainDataTable As String: MainDataTable = "Table1"                           'Should be the main table on the BDDS
Dim MainTable As ListObject: Set MainTable = WsCP.ListObjects(MainDataTable)    'Mimics synax to call on the main data table as a variable (to make things cleaner)
Dim WholeMainTable As Range

Dim Grp1Criteria As Range

Dim StartTime As Double
Dim ElapsedTime As Double
'
'Dim button_name As String: button_name = "Test"

'StartTime = MicroTimer

    WsSizes.Range("AD10:AP10").Calculate 'ensuring cells are updated before use
    WsSizes.Range("AD14:AJ14").Calculate 'ensuring cells are updated before use
    WsSizes.Range("AD16:AH16").Calculate 'ensuring cells are updated before use

    If WsSizes.Range("AD10").Value = 0 And WsSizes.Range("AD14").Value = 0 And WsSizes.Range("AD16").Value = 0 Then
        Debug.Print button_name & " - " & ProcName & " - " & " Filters NOT applied"
        GoTo SafeExit
            Else
            
            Call Clear_BDDS_Table
            
            WsDND.Range("BZ4:CS4").Calculate 'ensuring cells are updated before use
            
            Set WholeMainTable = WsCP.Range(WsCP.Range(TableHeaders), WsCP.Range(TableHeaders).End(xlDown))

            Set Grp1Criteria = WsDND.Range("BZ3").CurrentRegion
            
            WholeMainTable.AdvancedFilter xlFilterInPlace, Grp1Criteria
            
            Debug.Print button_name & " - " & ProcName & " - " & " Filters applied"
    End If

Moving the declaration of the range AFTER clearing the table fixed my issue.清除表后移动范围声明解决了我的问题。

Live and learn, hope this might help someone else in the future.生活和学习,希望这可以帮助将来的其他人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM