Sub autogroup()
Dim k
With ActiveSheet.Outline
.AutomaticStyles = False
.SummaryRow = xlAbove
.SummaryColumn = xlRight
End With
max_rows = ActiveSheet.UsedRange.Rows.Count
max_cols = ActiveSheet.UsedRange.Columns.Count
Range(Cells(1, 1), Cells(max_rows, 1)).ClearOutline
Range("A" & max_rows + 1).Value = 1
For ii = 2 To max_cols - 2
i = max_cols - ii
For j = 1 To max_rows + 1
If Application.CountIf(Range(Cells(j, 1), Cells(j, i)), "<>") = 0 Then
If (flag = 1) Then
k = k
Else
k = j
End If
flag = 1
ElseIf (flag = 1) Then
Range(Cells(k, 1), Cells(j - 1, 1)).Rows.Group
k = 1
flag = 0
End If
Next
Next
Range("A" & max_rows + 1).Value = ""
End Sub
Dim k
With ActiveSheet.Outline
.AutomaticStyles = False
.SummaryRow = xlAbove
.SummaryColumn = xlRight
End With
max_rows = ActiveSheet.UsedRange.Rows.Count
max_cols = ActiveSheet.UsedRange.Columns.Count
Range(Cells(1, 1), Cells(max_rows, 1)).ClearOutline
Range("A" & max_rows + 1).Value = 1
For ii = 2 To max_cols - 2
i = max_cols - ii
For j = 1 To max_rows + 1
If Application.CountIf(Range(Cells(j, 1), Cells(j, i)), "<>") = 0 Then
If (flag = 1) Then
k = k
Else
k = j
End If
flag = 1
ElseIf (flag = 1) Then
Range(Cells(k, 1), Cells(j - 1, 1)).Rows.Group
k = 1
flag = 0
End If
Next
Next
Range("A" & max_rows + 1).Value = ""
End Sub
本文介绍了一段使用VBA实现Excel中表格数据自动分组的代码。通过遍历指定范围内的数据,根据条件自动将相同的数据进行分组并隐藏不需要显示的细节行。该方法适用于需要快速整理大量数据的工作场景。

646

被折叠的 条评论
为什么被折叠?



