根据列中的字符串有条件地折叠 Excel 行?

Conditionally collpase Excel rows based on string in column?

我每周都会从项目管理工具中获得大量 excel sheet。它包含有关特定任务的特定日期和时间的信息。内容每周都会更改。但结构保持不变。它基本上是这样的:

+----------+---------------+----------+-----------------+--------------+-----------------+--------------+----------------------+--------------+----------------+-----------------+-------------------------------------+---------------+-------------+------------+---------------------------+-------------------+------------------+--------------+-------------------------+---------------+-----------------------+---------------------+------------+
|   Type   |      Hub      | Hub (ID) |     Project     | Project (ID) |     Package     | Package (ID) |       Measure        | Measure (ID) | Measure (Tags) | Measure (Phase) |              Activity               | Activity (ID) | Aggregation | To Do/Done |          Comment          |    Responsible    | Responsible (ID) | Start (Plan) | Start (Actual/Forecast) | Start (Delta) | Start (Traffic Light) | Start (Achievement) | End (Plan) |
+----------+---------------+----------+-----------------+--------------+-----------------+--------------+----------------------+--------------+----------------+-----------------+-------------------------------------+---------------+-------------+------------+---------------------------+-------------------+------------------+--------------+-------------------------+---------------+-----------------------+---------------------+------------+
| Hub      | ABC Comp Demo |      285 | -               |            - | -               |            - | -                    |            - |              - | -               | -                                   |             - | -           | To Do      | -                         | -                 |                - | -            | -                       |             - | -                     |                   - | -          |
| Project  | ABC Comp Demo |      285 | English Example |         1802 | -               |            - | -                    |            - |              - | -               | -                                   |             - | Active      | To Do      | -                         | Friederike Bieser |             1650 | 2020-05-19   | 2020-05-16              |            -3 | Yellow                |               81,00 | 2020-07-26 |
| Package  | ABC Comp Demo |      285 | English Example |         1802 | Working Capital |         9440 | -                    |            - |              - | -               | -                                   |             - | Active      | To Do      | -                         |                   |                  | 2020-05-19   | 2020-05-16              |            -3 | Yellow                |               71,50 | 2020-07-26 |
| Measure  | ABC Comp Demo |      285 | English Example |         1802 | Working Capital |         9440 | Inventory reduction  |        38894 |                | DOI3            | -                                   |             - | Active      | To Do      | -                         |                   |                  | 2020-05-29   | 2020-05-21              |            -8 | Yellow                |               66,67 | 2020-07-05 |
| Activity | ABC Comp Demo |      285 | English Example |         1802 | Working Capital |         9440 | Inventory reduction  |        38894 |                | DOI3            | Adjusting the term of payment       |        131741 | Active      | Done       |                           | Friederike Bieser |             1650 | 2020-05-29   | 2020-05-21              |            -8 | Green                 |              100,00 | 2020-06-05 |
| Activity | ABC Comp Demo |      285 | English Example |         1802 | Working Capital |         9440 | Inventory reduction  |        38894 |                | DOI3            | New controlling cycle               |        131742 | Active      | To Do      |                           | Friederike Bieser |             1650 | 2020-06-09   | 2020-06-06              |            -3 | Green                 |              100,00 | 2020-06-16 |
| Activity | ABC Comp Demo |      285 | English Example |         1802 | Working Capital |         9440 | Inventory reduction  |        38894 |                | DOI3            | ABC analysis                        |        131743 | Active      | To Do      |                           | Friederike Bieser |             1650 | 2020-06-17   | 2020-06-26              |             9 | Red                   |                0,00 | 2020-06-22 |
| Activity | ABC Comp Demo |      285 | English Example |         1802 | Working Capital |         9440 | Inventory reduction  |        38894 |                | DOI3            | Adjust cash discount agreements     |        131744 | Active      | To Do      | All good ...              | Friederike Bieser |             1650 | 2020-06-23   | 2020-06-26              |             3 | Yellow                |               33,33 | 2020-07-01 |
| Activity | ABC Comp Demo |      285 | English Example |         1802 | Working Capital |         9440 | Inventory reduction  |        38894 |                | DOI3            | Sell old stocks                     |        131745 | Active      | To Do      |                           | Friederike Bieser |             1650 | 2020-07-02   | 2020-07-02              |             0 | Green                 |              100,00 | 2020-07-05 |
| Measure  | ABC Comp Demo |      285 | English Example |         1802 | Working Capital |         9440 | Turnover             |        38895 |                | DOI2            | -                                   |             - | Active      | To Do      | -                         |                   |                  | 2020-05-19   | 2020-05-16              |            -3 | Yellow                |               77,78 | 2020-07-26 |
| Activity | ABC Comp Demo |      285 | English Example |         1802 | Working Capital |         9440 | Turnover             |        38895 |                | DOI2            | Inventory                           |        131746 | Active      | Done       |                           | Friederike Bieser |             1650 | 2020-05-19   | 2020-05-16              |            -3 | Green                 |              100,00 | 2020-06-07 |
| Activity | ABC Comp Demo |      285 | English Example |         1802 | Working Capital |         9440 | Turnover             |        38895 |                | DOI2            | Turnover                            |        131747 | Active      | To Do      | Everything is working ... | Friederike Bieser |             1650 | 2020-06-16   | 2020-06-15              |            -1 | Green                 |              100,00 | 2020-06-21 |
| Activity | ABC Comp Demo |      285 | English Example |         1802 | Working Capital |         9440 | Turnover             |        38895 |                | DOI2            | Evaluation of old inventory         |        131748 | Active      | To Do      |                           | Friederike Bieser |             1650 | 2020-06-22   | 2020-06-28              |             6 | Yellow                |               33,33 | 2020-06-27 |
| Activity | ABC Comp Demo |      285 | English Example |         1802 | Working Capital |         9440 | Turnover             |        38895 |                | DOI2            | Sell of old inventory               |        131749 | Active      | To Do      |                           | Friederike Bieser |             1650 | 2020-06-27   | 2020-07-03              |             6 | Yellow                |               33,33 | 2020-07-04 |
| Activity | ABC Comp Demo |      285 | English Example |         1802 | Working Capital |         9440 | Turnover             |        38895 |                | DOI2            | Allocation of storage areas         |        131750 | Active      | To Do      | Risk ...                  | Friederike Bieser |             1650 | 2020-07-05   | 2020-07-02              |            -3 | Green                 |              100,00 | 2020-07-15 |
| Activity | ABC Comp Demo |      285 | English Example |         1802 | Working Capital |         9440 | Turnover             |        38895 |                | DOI2            | Integration of SAP in storage areas |        131751 | Active      | To Do      |                           | Friederike Bieser |             1650 | 2020-07-18   | 2020-07-20              |             2 | Green                 |              100,00 | 2020-07-26 |
| Package  | ABC Comp Demo |      285 | English Example |         1802 | Production      |         9441 | -                    |            - |              - | -               | -                                   |             - | Active      | To Do      | -                         |                   |                  | 2020-05-29   | 2020-06-03              |             5 | Yellow                |               86,00 | 2020-07-13 |
| Measure  | ABC Comp Demo |      285 | English Example |         1802 | Production      |         9441 | Machinery hall       |        38896 |                | DOI3            | -                                   |             - | Active      | To Do      | -                         |                   |                  | 2020-05-29   | 2020-06-03              |             5 | Yellow                |               86,67 | 2020-07-13 |
| Activity | ABC Comp Demo |      285 | English Example |         1802 | Production      |         9441 | Machinery hall       |        38896 |                | DOI3            | Sell milling cutter long            |        131752 | Active      | Done       | All good ...              | Friederike Bieser |             1650 | 2020-05-29   | 2020-06-03              |             5 | Yellow                |               33,33 | 2020-06-03 |
| Activity | ABC Comp Demo |      285 | English Example |         1802 | Production      |         9441 | Machinery hall       |        38896 |                | DOI3            | Sell extruder                       |        131753 | Active      | To Do      |                           | Friederike Bieser |             1650 | 2020-06-20   | 2020-06-17              |            -3 | Green                 |              100,00 | 2020-06-25 |
| Activity | ABC Comp Demo |      285 | English Example |         1802 | Production      |         9441 | Machinery hall       |        38896 |                | DOI3            | Purchase milling machine 34DDe      |        131754 | Active      | To Do      |                           | Friederike Bieser |             1650 | 2020-06-26   | 2020-06-26              |             0 | Green                 |              100,00 | 2020-06-30 |
| Activity | ABC Comp Demo |      285 | English Example |         1802 | Production      |         9441 | Machinery hall       |        38896 |                | DOI3            | Introduction milling machine 34DDe  |        131755 | Active      | To Do      | Risk ...                  | Friederike Bieser |             1650 | 2020-07-01   | 2020-07-02              |             1 | Green                 |              100,00 | 2020-07-02 |
| Activity | ABC Comp Demo |      285 | English Example |         1802 | Production      |         9441 | Machinery hall       |        38896 |                | DOI3            | Evaluation                          |        131756 | Active      | To Do      |                           | Friederike Bieser |             1650 | 2020-07-03   | 2020-06-30              |            -3 | Green                 |              100,00 | 2020-07-13 |
| Package  | ABC Comp Demo |      285 | English Example |         1802 | Procurement     |         9442 | -                    |            - |              - | -               | -                                   |             - | Active      | To Do      | -                         |                   |                  | 2020-05-24   | 2020-05-24              |             0 | Yellow                |               86,00 | 2020-07-22 |
| Measure  | ABC Comp Demo |      285 | English Example |         1802 | Procurement     |         9442 | Diversification      |        38897 |                | DOI1            | -                                   |             - | Active      | To Do      | -                         |                   |                  | 2020-05-24   | 2020-05-24              |             0 | Yellow                |               86,67 | 2020-07-22 |
| Activity | ABC Comp Demo |      285 | English Example |         1802 | Procurement     |         9442 | Diversification      |        38897 |                | DOI1            | ABC analysis                        |        131757 | Active      | Done       | All good ...              | Friederike Bieser |             1650 | 2020-05-24   | 2020-05-24              |             0 | Green                 |              100,00 | 2020-06-06 |
| Activity | ABC Comp Demo |      285 | English Example |         1802 | Procurement     |         9442 | Diversification      |        38897 |                | DOI1            | Value Network with Meyer Company    |        131758 | Active      | To Do      |                           | Friederike Bieser |             1650 | 2020-06-14   | 2020-06-14              |             0 | Green                 |              100,00 | 2020-06-22 |
| Activity | ABC Comp Demo |      285 | English Example |         1802 | Procurement     |         9442 | Diversification      |        38897 |                | DOI1            | Renegotiation                       |        131759 | Active      | To Do      |                           | Friederike Bieser |             1650 | 2020-06-23   | 2020-06-28              |             5 | Yellow                |               33,33 | 2020-07-13 |
| Activity | ABC Comp Demo |      285 | English Example |         1802 | Procurement     |         9442 | Diversification      |        38897 |                | DOI1            | Meeting                             |        131760 | Active      | To Do      |                           | Friederike Bieser |             1650 | 2020-07-14   | 2020-07-12              |            -2 | Green                 |              100,00 | 2020-07-19 |
| Activity | ABC Comp Demo |      285 | English Example |         1802 | Procurement     |         9442 | Diversification      |        38897 |                | DOI1            | Product group concept               |        131761 | Active      | To Do      |                           | Friederike Bieser |             1650 | 2020-07-20   | 2020-07-20              |             0 | Green                 |              100,00 | 2020-07-22 |
| Measure  | ABC Comp Demo |      285 | English Example |         1802 | Procurement     |         9442 | Supplier Negotiation |        38898 |                |                 | -                                   |             - | Paused      |            | -                         |                   |                             
+----------+---------------+----------+-----------------+--------------+-----------------+--------------+----------------------+--------------+----------------+-----------------+-------------------------------------+---------------+-------------+------------+---------------------------+-------------------+------------------+--------------+-------------------------+---------------+-----------------------+---------------------+------------+

有没有一种方法可以根据第 Type 列中的字符串有条件地折叠行?我希望折叠 Type 列中包含字符串 Activity 的所有行。

如果你能帮助我,那就太好了!提前致谢!

编辑:

Select A 列并找到 (Control + F) "Activity" .. Select 匹配整个单元格内容 --> 单击 "Find All" --> 按 Control + A到select对话框中列表中的所有范围。

按 Control + 9 隐藏行或运行以下过程。

Sub Macro1()
    Dim ActGrps As Range
    Set ActGrps = Selection
    Dim ar As Range
    For Each ar In ActGrps.Areas
    ar.Rows.Group
    ar.Rows.Hidden = True
    Next
End Sub

使用命令按钮select编辑 "Activity" 单元格并对行进行分组可能需要更长的过程才能完全自动化所有这些操作。

一次显示和隐藏所有行 - Select 数据 A 列(在本例中为 A1:A32)。单击数据菜单 --> 大纲 --> Show/Hide 详细信息。 同样,您也可以使用菜单 --> 大纲 --> 取消分组 --> 行一次取消所有数据的分组。

要显示或隐藏特定组,请使用左侧带有行号的句柄。

编辑 以下是根据以下评论添加的过程。

Public fndAdd As String
'========================================================================
Sub GroupRowsWithCondition()
Dim sh As Worksheet
Set sh = ActiveSheet
Dim DataCol As Range
Set DataCol = sh.Range("A1:A" & Range("A" & sh.Rows.Count).End(xlUp).Row)
'Debug.Print DataCol.Address '$A:$A

Dim cl As Range, fnd As Range
    For Each cl In DataCol
    If cl = "Activity" Then
        If fnd Is Nothing Then
        Set fnd = cl
        Else
        Set fnd = Union(fnd, cl)
        End If
    End If
    Next
'Debug.Print fnd.Address '$A:$A,$A:$A,$A:$A,$A:$A

fndAdd = fnd.Address

Dim ar As Range
For Each ar In fnd.Areas
ar.Rows.Group
'ar.Rows.Hidden = True
Next

Range("A1").Select
End Sub
'========================================================================

Sub toggleGroupShowHide()
'If assigned to form control button on a sheet,
' this macro will show / hide rows of the group created in GroupRowsWithCondition procedure
With ActiveSheet.Range(fndAdd)
    If .Rows.Hidden = True Then
        .Rows.Hidden = False
    Else
        .Rows.Hidden = True
    End If
End With

End Sub
'========================================================================

建议避免使用 ActiveSheet,改用 sheet 名称。所以,Set sh = Sheets("Sheet1")Set sh = ActiveSheet

更好

您可以使用开发人员菜单 --> 控件 --> 插入 --> 表单控件 --> 按钮在 sheet 上插入一个按钮并将宏分配给该按钮。但是如果经常使用这个宏,可以考虑一下。

这个VBA-代码

Sub Group_Activity()

' Groups all rows with "Activity" in Column "A"
Dim cell As Range
Dim lastrow As Long
Dim startrow As Long
Dim endrow As Long

startrow = -1
endrow = -1

    With ActiveSheet

        lastrow = .Cells(.Rows.Count, 1).End(xlUp).Row

        For Each cell In .Range("A1:A" & lastrow)
            'Debug.Print Trim(cell.Value)
            If Trim(cell.Value) <> "Activity" Then
                If startrow > 0 Then
                    .Rows(startrow & ":" & endrow).Group
                    startrow = -1
                    endrow = -1
                End If

            Else
                If startrow = -1 Then startrow = cell.Row
                endrow = cell.Row

            End If
        Next
    End With
End Sub

在没有任何选择过程的情况下得出这个结果: