2017-08-10 57 views
0

我想通過VBA在Excel中基於它們的格式對行進行分組,並且在谷歌搜索時發現了一個2005年的論壇帖子,其中提出了類似的問題。
用戶發佈的代碼對於選擇行非常適用,但我無法獲得代碼來實際分組行。基於格式的VBA分組行

Original Forum post

代碼:

Sub GetSameFormattedRange() 
    Dim c As Range, r As Range, e As Range 

    Set e = Intersect(ActiveSheet.UsedRange, Columns(1)) 

    SetFormatData 

    Set c = e.Find(What:="", SearchFormat:=True) 

    If Not c Is Nothing Then 
     Set r = c 
     firstAddress = c.Address 
     Do 
      Set r = Union(r, c) 
      Set c = e.FindNext(After:=c) 

      If Not c Is Nothing Then 
       If c.Address = firstAddress Then Exit Do 
      End If 
     Loop While Not c Is Nothing 
    End If 

    If Not r Is Nothing Then 
     MsgBox r.Address 
    End If 

    Application.FindFormat.Clear 
End Sub 

Sub SetFormatData() 
    Application.FindFormat.Clear 
    Application.FindFormat.Interior.Color = vbRed 

    Application.FindFormat.Locked = True 
    Application.FindFormat.FormulaHidden = False 
End Sub 

在我的情況Application.FindFormat.Interior.Color = vbRedApplication.FindFormat.IndentLevel = 1

謝謝!

回答

0

這可能不是爲你工作,但我實際上做類似的事情今天早些時候,我在那裏分組基於.Interior.Pattern項目,這也應該.Interior.Color工作:

Dim i as Integer 
For i = 2 to 10 
    If Cells(i,1).Interior.Pattern=xlSolid Then 
     Rows(i).EntireRow.Group 
     Else 
     End If 
    Next i 

的對此要注意的是,每一行都是分組的,如果後續行也分組,那麼它們配對在一起,從而增加分組行的總長度。如果有休息,它開始一個新的分組。

+0

太棒了!必須調整一下,但非常感謝答案。花幾個小時試圖找到一個更復雜的方式來做到這一點。 – GenTugorn