本文介绍了通过Excel VBA根据父子关系进行分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我的项目有n个项目,并且每个项目作为一个父项分配给他们.我想根据亲子关系将其分组.在第一个屏幕截图中,行号769的子级从770到896 .因此,我想要我的结果作为屏幕快照2,该组将769下的行从770分组到896 示例结果需要

Suppose my project has n number of items and each item as one parent assign to them. I want to group the same according to parent child relationship.In the 1st screenshoot the row number 769 has child from 770 to 896. So i want my result as Screenshot 2 which group row from 770 to 896 under 769 Sample result require

推荐答案

将此代码添加到新模块中...

Add this code to a new module ...

Public Sub PerformOutlineOnSelectedRows()
    Dim i As Long, lngLevel As Long

    Selection.Rows.ClearOutline

    For i = 1 To Selection.Rows.Count
        lngLevel = UBound(Split(Selection.Cells(i, 1), ".")) + 1

        If lngLevel > 8 Then lngLevel = 8

        Selection.Rows(i).EntireRow.OutlineLevel = lngLevel
    Next
End Sub

...,然后选择要分组和概述的行,然后从功能区中的开发人员选项卡运行宏.

... then select the rows you want to group and outline and then run the macro from the Developer tab in the ribbon.

请记住,分组和大纲功能仅会达到8个级别,因此低于此级别的任何内容将无法正确分组.

Keep in mind, the group and outline functionality only goes to 8 levels so anything below that won't group properly.

让我知道它是否有效,或者您需要更多信息.

Let me know if it works or if you need more info.

这篇关于通过Excel VBA根据父子关系进行分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 21:37