我有一个包含数字和空白单元格的列。我设置的过滤器应该隐藏空白/空的单元格;但是,它似乎没有正常工作。出于某种原因,它似乎隐藏了空白单元格,直到Autofiltermode设置为false。请参阅下面的代码。
With ActiveWorkbook.Worksheets("Sheet1")
.AutoFilterMode = False
With .Range("A:BM")
.AutoFilter Field:=65, Criteria1:=""
On Error Resume Next
.Resize(.Rows.Count - 1).Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Hidden
On Error GoTo 0
End With
.AutoFilterMode = False
答案 0 :(得分:0)
设置.Autofilter = False会删除过滤器,以便您可以看到隐藏的行。
而是查看EntireRow.Hidden = True
和False
以隐藏与当前过滤器无关的行。
您可以在删除过滤器之前将隐藏的行收集到范围对象的并集中,以便稍后可以在任何时候隐藏它们。
注意我正在设置测试范围.Range("A1:A5")
。这将在您的数据中设置为最后一个感兴趣的行。
对于最后一行方法,请参阅Ron De Bruin的articles。
Sub test()
With ActiveWorkbook.Worksheets("Sheet1")
.AutoFilterMode = False
With .Range("A:BM")
.AutoFilter Field:=65, Criteria1:=vbNullstring
Dim rng As Range
Set rng = .Range("A1:A5")
Dim aRow As Range
Dim unionRng As Range
For Each aRow In rng.Rows
If aRow.EntireRow.Hidden = False Then 'were hidden because were blanks
If Not unionRng Is Nothing Then
Set unionRng = Union(unionRng, aRow) 'store the "" rows in union range
Else
Set unionRng = aRow
End If
End If
Next aRow
End With
.AutoFilterMode = False
If Not unionRng Is Nothing Then MsgBox unionRng.Address
End With
End Sub
隐藏你现在可以随时说:
If Not unionRng Is Nothing Then unionRng.EntireRow.Hidden = True
答案 1 :(得分:0)
不要像评论中指出的那样使用Autofilter。试试这段代码。
Dim i As Long
Dim lRow As Long
lRow = Cells(Rows.Count, "A").End(xlUp).Row
For i = 1 To lRow
Cells(i, 65).EntireRow.Hidden = IsEmpty(Cells(i, 65).Value)
Next i
'do other checks using SpecialCells(xlCellTypeVisible)
或者
Dim i As Long
Dim lRow As Long
lRow = Cells(Rows.Count, "A").End(xlUp).Row
For i = 1 To lRow
If Cells(i, 65).Value = "" Then
Cells(i, 65).EntireRow.Hidden = True
End If
Next i