VBA新手。我试图将A列中的所有行分组为FALSE。下面的代码适用于分组空白。是否存在TRUE / FALSE的SpecialCells函数,还是应该使用其他函数?
Dim rng As Range
Dim falseRange As Range
Dim grp As Range
Set rng = Range("a1", Cells(Rows.Count, 1).End(xlUp))
Set falseRange = rng.SpecialCells(xlCellTypeBlanks)
For Each grp In falseRange
grp.Rows.Group
Next
答案 0 :(得分:0)
你可以用这个:
Option Explicit
Sub main()
Dim falseRange As Range
Dim grp As Range
With Range("A1", Cells(Rows.count, 1).End(xlUp).Offset(1))
.AutoFilter field:=1, Criteria1:=CStr(False), Operator:=xlAnd, Criteria2:="<>"
If Application.WorksheetFunction.Subtotal(103, .Cells) > 1 Then
Set falseRange = .Resize(.Rows.count - 1).Offset(1).SpecialCells(xlCellTypeVisible)
End If
.Parent.AutoFilterMode = False
End With
For Each grp In falseRange.Areas '<--| group contiguous cells toghether
grp.Rows.Group
Next
End Sub
重构可能是:
Sub main()
Dim falseRange As Range
Dim grp As Range
Set falseRange = GetFalseRange(Range("A1", Cells(Rows.count, 1).End(xlUp).Offset(1)))
If Not falseRange Is Nothing Then
For Each grp In falseRange.Areas '<--| group contiguous cells toghether
grp.Rows.Group
Next
End If
End Sub
Function GetFalseRange(rng As Range) As Range
With rng
.AutoFilter field:=1, Criteria1:=CStr(False), Operator:=xlAnd, Criteria2:="<>"
If Application.WorksheetFunction.Subtotal(103, .Cells) > 1 Then Set GetFalseRange = .Resize(.Rows.count - 1).Offset(1).SpecialCells(xlCellTypeVisible)
.Parent.AutoFilterMode = False
End With
End Function