2017-03-18 19 views
0

VBAの新機能。私はFALSEである列Aのすべての行をグループ化しようとしています。以下のコードは、空白をグループ化するためのコードです。 TRUE/FALSEのSpecialCells関数がありますか、別の関数を使用する必要がありますか?VBAグループ行TRUE/FALSE条件

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

あなたはこれを使用することができます。

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 
+0

パーフェクト!どうもありがとうございます。 – MaineBlackBears

+0

ようこそ。私のコードを少しリファクタリングして、それをもっと "使える"ようにしてください。 – user3598756

+0

リファクタリングは私にとって意味があります。ありがとうございました。 – MaineBlackBears