2017-09-01 66 views
2

细胞我wnat做使用VBA分组与VBA

在Excel中一些分组

我的“标准”一栏是“A”,这是普遍认为是明显多余的,应归给用户号码的列表更好地了解excel表格

我已命名列“A”“Vertrag__Nr”。

我的代码

Sub Test() 
Dim i As Integer, LastRow As Integer 

LastRow = Cells(Rows.Count, 1).End(xlUp).Row 

For i = 1 To LastRow 

    If Not Left(Cells(i, 1), 2) = "Vertrag__Nr." Then 
     Cells(i, 2).EntireRow.Group 
    End If 

Next i 
End Sub 

我的问题是我的代码,而不是由项目组 “Vertrag _Nr。” (柱A)。在基团的整列到一个大GROUPE

enter image description here

+0

从我所看到的,你的代码会做一个大集团。你能提供你的工作表的快照吗? – Zac

+0

@zac我已更新我的问题 –

+0

因此,在您提供的示例中,哪些行应该分组? –

回答

1

作为分组被用于摘要,必须有用于组之间摘要的地方,它们可以是不连续的,尝试此代码:

Sub Test() 
Dim i As Integer, j As Integer, LastRow As Integer 
Dim currVal As Variant 
    With ActiveSheet 
     LastRow = .Cells(Rows.Count, 1).End(xlUp).Row 
     i = 2 

     While i <= LastRow 
      currVal = .Cells(i, 1).Value 
      j = 0 
      Do 
       j = j + 1 
      Loop Until .Cells(i + j, 1).Value <> currVal 
      If j > 1 Then 
       .Rows(i + j).Insert 
       .Cells(i + j, 1).Value = currVal 
       Range(.Cells(i, 1), .Cells(i + j - 1, 1)).EntireRow.Group 
      End If 
      i = i + j 
     Wend 
    End With 
End Sub 
+0

多数民众赞成在此非常感谢你的时间和精力! :)$ –