vba - 使用 VBA 在单元格中分组

标签 vba excel

我想使用 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 列)将整列分组为一个大组

enter image description here

最佳答案

由于分组用于汇总,因此组之间必须有一个汇总的地方,它们不能是连续的,试试这个代码:

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

关于vba - 使用 VBA 在单元格中分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45994789/

相关文章:

ms-access - Access 权限 : Proper use of Nz in a query?

excel - 为什么我的 Do Until 循环只打印循环中的最后一个值?

html - 使用VBA从网页中提取表格

vba - Excel VBA 创建按钮以复制/重命名工作表和整个工作簿

Excel 宏 VBA 未指示

excel - 处理输入框取消选择范围的操作

VBA:从函数返回 Range.Find() 结果

excel - 如何更改指定列中的单元格颜色?

excel - 如何在匹配另一个单元格的同时查找一个单元格

delphi - 尝试访问 Delphi 7 中的 Excel 表时的提供程序选择