首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >按值分组行

问按值分组行
EN

Stack Overflow用户
提问于 2016-01-16 07:14:47
回答 1查看 5.3K关注 0票数 2

如何以编程方式按列B中的值对数据进行分组

注意,A和C列中存在随机值。

​

​

如下所示:

​

-->

​

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-01-16 08:38:33

尝尝这个

代码语言:javascript
复制
Sub demo()
    Dim r As Range
    Dim v As Variant
    Dim i As Long, j As Long

    With ActiveSheet
        On Error Resume Next
        ' expand all groups on sheet
        .Outline.ShowLevels RowLevels:=8
        ' remove any existing groups
        .Rows.Ungroup
        On Error GoTo 0
        Set r = .Range("B1", .Cells(.Rows.Count, 2).End(xlUp))
    End With

    With r
        'identify common groups in column B
        j = 1
        v = .Cells(j, 1).Value
        For i = 2 To .Rows.Count
            If v <> .Cells(i, 1) Then
                ' Colum B changed, create group
                v = .Cells(i, 1)
                If i > j + 1 Then
                    .Cells(j + 1, 1).Resize(i - j - 1, 1).Rows.Group
                End If
                j = i
                v = .Cells(j, 1).Value
            End If
        Next
        ' create last group
        If i > j + 1 Then
            .Cells(j + 1, 1).Resize(i - j - 1, 1).Rows.Group
        End If
        ' collapse all groups
        .Parent.Outline.ShowLevels RowLevels:=1
    End With
End Sub
票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34824529

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档