Cap*_*ABC 12 excel vba excel-vba
我想这很简单,但由于某些原因它似乎对我不起作用:(
我有以下代码,它根据我指定的条件自动过滤数据:
Dim lastrow As Long
lastrow = Sheet2.Cells(Sheet2.Rows.Count, "A").End(xlUp).Row
With Sheet2
.AutoFilterMode = False
With .Range("A1:AF" & lastrow)
.AutoFilter
.AutoFilter Field:=7, Criteria1:="Yes", Operator:=xlFilterValues
End With
Run Code Online (Sandbox Code Playgroud)
我现在要做的是删除所有不符合条件的Unfiltered(Hidden)行.
我到目前为止尝试过:
Sub RemoveHiddenRows
Dim oRow As Object
For Each oRow In Sheets("Sheet2").Rows
If oRow.Hidden Then oRow.Delete
Next
End Sub
Run Code Online (Sandbox Code Playgroud)
但是这个代码的问题是它只会删除连续隐藏行的每一行,因为即使删除了一行并且所有较低的行都向上移动了一行,每个行都会增加所考虑的行.
如果可能的话,我更喜欢没有循环的东西,有点像相反的东西.SpecialCells(xlCellTypeVisible).EntireRow.Delete
所有帮助将受到高度赞赏.
Dmi*_*liv 21
因此,我有点想要摆脱未过滤的数据,而不是试图扭转所有标准并删除可见的单元格
我会用这个:
Sub RemoveHiddenRows()
Dim oRow As Range, rng As Range
Dim myRows As Range
With Sheets("Sheet3")
Set myRows = Intersect(.Range("A:A").EntireRow, .UsedRange)
If myRows Is Nothing Then Exit Sub
End With
For Each oRow In myRows.Columns(1).Cells
If oRow.EntireRow.Hidden Then
If rng Is Nothing Then
Set rng = oRow
Else
Set rng = Union(rng, oRow)
End If
End If
Next
If Not rng Is Nothing Then rng.EntireRow.Delete
End Sub
Run Code Online (Sandbox Code Playgroud)