我想使用 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 列)将整列分组为一个大组
最佳答案
由于分组用于汇总,因此组之间必须有一个汇总的地方,它们不能是连续的,试试这个代码:
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/