简体   繁体   English

ActiveSheet.ShowAllData在2台计算机上返回不同的结果

[英]ActiveSheet.ShowAllData returns different results on 2 machines

I have some macro that filters all the data (250 rows) by one column, then deletes the data that is hidden after filtering. 我有一些宏可以按一列过滤所有数据(250行),然后删除过滤后隐藏的数据。

Next step is unfiltering whole sheet using ActiveSheet.ShowAllData. 下一步是使用ActiveSheet.ShowAllData取消过滤整个工作表。

What's strange, on my machine after unfiltering my used range seems to be 200 rows (50 rows has been deleted). 奇怪的是,在我的机器上,对使用范围进行了过滤之后,似乎是200行(其中50行已被删除)。

However on another machine, with the same Excel version (Office365), after unfiltering and deleting 50 rows the visible range is 1,048,576 rows (what pretty much messes up further autofills etc.) 但是,在另一台具有相同Excel版本(Office365)的计算机上,取消过滤并删除50行后,可见范围为1,048,576行(​​这几乎会使进一步的自动填充等混乱)。

The code is as follows: 代码如下:

Dim lastRow As Long
Dim iCntr As Long

Range("A:AI" & Lines).AutoFilter Field:=32, Criteria1:= _
    "2611"
lastRow = Cells(Rows.Count, 1).End(xlUp).Row
For iCntr = lastRow To 1 Step -1
    If Rows(iCntr).Hidden = True Then Rows(iCntr).EntireRow.Delete
Next

ActiveSheet.ShowAllData

I recommend to collect all the rows that need to be deleted in a variable RowsToDelete and then show all data before you delete all the rows at once. 我建议将所有需要删除的行收集到变量RowsToDelete ,然后显示所有数据,然后一次删除所有行。 Not sure if this finally fixes your issue but at least this is a lot faster than deleting each row after another. 不知道这是否最终解决了您的问题,但至少比删除每一行要快得多。

Option Explicit

Public Sub DeleteAllHiddenRows()
    Dim ws As Worksheet
    Set ws = ActiveSheet

    Dim LastRow As Long
    LastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row

    ws.Range("A:AI").AutoFilter Field:=32, Criteria1:="2611"

    Dim RowsToDelete As Range

    Dim iRow As Long
    For iRow = 1 To LastRow
        If ws.Rows(iRow).Hidden Then
            If RowsToDelete Is Nothing Then
                Set RowsToDelete = ws.Rows(iRow)
            Else
                Set RowsToDelete = Union(RowsToDelete, ws.Rows(iRow))
            End If
        End If
    Next iRow

    ws.ShowAllData
    RowsToDelete.EntireRow.Delete
End Sub
Sub clear_filter()
Dim sh As Worksheet



sh=activeworksheet
If sh.AutoFilterMode Then

 On Error GoTo 1
  sh.ShowAllData
    sh.AutoFilterMode = False
End If

1

MsgBox "Data filters cleared", vbInformation


    End Sub

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

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