Excel分组的深度有限制吗?

发布于 2024-12-19 02:31:42 字数 926 浏览 2 评论 0原文

我正在尝试在 Excel 中编写一个宏,它允许我根据第一列中的数字自动进行分组。这是代码。

Sub Makro1()
Dim maxRow As Integer
Dim row As Integer
Dim groupRow As Integer
Dim depth As Integer
Dim currentDepth As Integer

maxRow = Range("A65536").End(xlUp).row

For row = 1 To maxRow
    depth = Cells(row, 1).Value
    groupRow = row + 1
    currentDepth = Cells(groupRow, 1).Value
    If depth >= currentDepth Then
       GoTo EndForLoop
    End If
    Do While currentDepth > depth And groupRow <= maxRow
        groupRow = groupRow + 1
        currentDepth = Cells(groupRow, 1).Value
    Loop
    Rows(row + 1 & ":" & groupRow - 1).Select
    Selection.Rows.Group
EndForLoop:
    Next row
End Sub

Excel 文件中的第一列如下所示:

1
2
2
3
3
4
4
4
4
5
5
5
6
6
6
6
5
6
6
6
7
8
8
9
10
9
10
10
8
7
7
8
6
5
4
3
2
1
2

当宏达到分组深度 8 时,我收到错误号 1004。看起来 Excel 不允许我创建大于 8 的深度。有解决方法吗为了这?我使用的是 MS Excel 2003。

I am trying to write a macro in Excel which will allow me to automatically do groupings based on the number located in the first column. Here is the code.

Sub Makro1()
Dim maxRow As Integer
Dim row As Integer
Dim groupRow As Integer
Dim depth As Integer
Dim currentDepth As Integer

maxRow = Range("A65536").End(xlUp).row

For row = 1 To maxRow
    depth = Cells(row, 1).Value
    groupRow = row + 1
    currentDepth = Cells(groupRow, 1).Value
    If depth >= currentDepth Then
       GoTo EndForLoop
    End If
    Do While currentDepth > depth And groupRow <= maxRow
        groupRow = groupRow + 1
        currentDepth = Cells(groupRow, 1).Value
    Loop
    Rows(row + 1 & ":" & groupRow - 1).Select
    Selection.Rows.Group
EndForLoop:
    Next row
End Sub

The first column in the Excel file looks like this:

1
2
2
3
3
4
4
4
4
5
5
5
6
6
6
6
5
6
6
6
7
8
8
9
10
9
10
10
8
7
7
8
6
5
4
3
2
1
2

When the macro reaches the depth 8 speaking of the groupings, I get error number 1004. It looks like the Excel does not allow me to create a depth greater than 8. Is there a workaround for this? I am using MS Excel 2003.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

岁月静好 2024-12-26 02:31:43

你运气不好。

分组有 8 级限制

  • 同时存在于 xl07
  • 在我的测试中存在于 xl2010 中(给出“范围类的组方法 失败的”)

You are out of luck.

There is an 8 level limit for grouping which

  • also exists in xl07
  • on my testing exists in xl2010 (gives "Group method of range class failed")
白馒头 2024-12-26 02:31:43

我编写此代码是为了隐藏子级别行,就像分组一样。

它需要第一行为空,其中将放置常规级别按钮。
它将为每个具有子级别的节点创建一个按钮(放置在第一列中)。
单击按钮将隐藏/取消隐藏相应的子级别。

  • check_col 是必须填充到最后一行的列(即没有空白行,否则“while”循环将停止
  • lvl_col 是包含级别索引的列
  • start_row 是包含有用数据的第一行

希望这个帮助

Sub group_tree()
check_col = "A"
lvl_col = "D"
start_row = 3


Dim btn As Button
Application.ScreenUpdating = False
ActiveSheet.Buttons.Delete

Dim t As Range

'------------Place the buttons on top--------------
i = start_row
e_lvl = 0
b_spac = 0
b_width = 20
b_toggle = 0
While Range(check_col & i) <> ""
    lvl = Range(lvl_col & i)
    If lvl > e_lvl Then e_lvl = lvl
i = i + 1
Wend

Set t = ActiveSheet.Range("A" & 1)
For c = Range(lvl_col & start_row) To e_lvl
    Set btn = ActiveSheet.Buttons.Add(t.Left + b_spac, t.Top, b_width, 10)
    With btn
    .OnAction = "btnS_t"
    .Caption = c
    .Name = start_row & "_" & c & "_" & lvl_col & "_" & b_toggle
    End With
    b_spac = b_spac + 20
Next

'--------------Place the buttons at level---------

i = start_row
While Range(check_col & i) <> ""
    lvl = Range(lvl_col & i)
    If Range(lvl_col & i + 1) > lvl Then
    Set t = ActiveSheet.Range("A" & i)
    '    Set btn = ActiveSheet.Buttons.Add(t.Left, t.Top, t.Width, t.Height)
    Set btn = ActiveSheet.Buttons.Add(t.Left, t.Top, b_width, 10)
    With btn
      .OnAction = "btnS"
      .Caption = lvl
      .Name = i & "_" & lvl & "_" & lvl_col
    End With
    End If
    i = i + 1
Wend
  Application.ScreenUpdating = True
End Sub

Sub btnS()
    Dim but_r As Integer
    Set b = ActiveSheet.Buttons(Application.Caller)
    id_string = b.Name

    Dim id() As String
    id = Split(id_string, "_")
    start_row = CInt(id(0))
    start_lvl = CInt(id(1))
    lvl_col = id(2)

'    MsgBox (lvl_col)
    Call hide_rows(start_lvl, start_row, lvl_col)
End Sub

Sub hide_rows(start_lvl, start_row, lvl_col)
    a = start_row + 1
    While Range(lvl_col & a) > start_lvl
    a = a + 1
    Wend

    If Range(lvl_col & start_row + 1, lvl_col & a - 1).EntireRow.Hidden = False Then
    Range(lvl_col & start_row + 1, lvl_col & a - 1).EntireRow.Hidden = True
    Else
    Range(lvl_col & start_row + 1, lvl_col & a - 1).EntireRow.Hidden = False
    End If


End Sub
Sub btnS_t()
    Dim but_r As Integer
    Set b = ActiveSheet.Buttons(Application.Caller)
    id_string = b.Name

    Dim id() As String
    id = Split(id_string, "_")
    start_row = CInt(id(0))
    start_lvl = CInt(id(1))
    lvl_col = id(2)
    b_toggle = CInt(id(3))

    If b_toggle = 0 Then
    b_toggle = 1
    Else
    b_toggle = 0
    End If

    b.Name = start_row & "_" & start_lvl & "_" & lvl_col & "_" & b_toggle

    Call hide_rows_tot(start_lvl, start_row, lvl_col, b_toggle)
End Sub
Sub hide_rows_tot(start_lvl, start_row, lvl_col, b_toggle)

    a = start_row

    While Range(lvl_col & a) <> ""
    b = a
    While Range(lvl_col & b) > start_lvl
    b = b + 1
    Wend

    If b > a Then
    If b_toggle = 1 Then
        Range(lvl_col & a, lvl_col & b - 1).EntireRow.Hidden = True
    Else
        Range(lvl_col & a, lvl_col & b - 1).EntireRow.Hidden = False
    End If

    a = b - 1
    End If
    a = a + 1
    Wend



End Sub

I wrote this code to hide the sublevel rows, like grouping does.

it needs the first row empty, where the general level buttons will be placed.
it will create a button (placed in the first column) for each node with sublevels.
Clicking on the buttons will hide/unhide the corresponding sublevels.

  • the check_col is a colum that must be filled up to the last rows (i.e. no blank rows, or the "while" loop will stop
  • the lvl_col is the column that contains the level index
  • the start_row is the first row that contains useful data

hope this helps

Sub group_tree()
check_col = "A"
lvl_col = "D"
start_row = 3


Dim btn As Button
Application.ScreenUpdating = False
ActiveSheet.Buttons.Delete

Dim t As Range

'------------Place the buttons on top--------------
i = start_row
e_lvl = 0
b_spac = 0
b_width = 20
b_toggle = 0
While Range(check_col & i) <> ""
    lvl = Range(lvl_col & i)
    If lvl > e_lvl Then e_lvl = lvl
i = i + 1
Wend

Set t = ActiveSheet.Range("A" & 1)
For c = Range(lvl_col & start_row) To e_lvl
    Set btn = ActiveSheet.Buttons.Add(t.Left + b_spac, t.Top, b_width, 10)
    With btn
    .OnAction = "btnS_t"
    .Caption = c
    .Name = start_row & "_" & c & "_" & lvl_col & "_" & b_toggle
    End With
    b_spac = b_spac + 20
Next

'--------------Place the buttons at level---------

i = start_row
While Range(check_col & i) <> ""
    lvl = Range(lvl_col & i)
    If Range(lvl_col & i + 1) > lvl Then
    Set t = ActiveSheet.Range("A" & i)
    '    Set btn = ActiveSheet.Buttons.Add(t.Left, t.Top, t.Width, t.Height)
    Set btn = ActiveSheet.Buttons.Add(t.Left, t.Top, b_width, 10)
    With btn
      .OnAction = "btnS"
      .Caption = lvl
      .Name = i & "_" & lvl & "_" & lvl_col
    End With
    End If
    i = i + 1
Wend
  Application.ScreenUpdating = True
End Sub

Sub btnS()
    Dim but_r As Integer
    Set b = ActiveSheet.Buttons(Application.Caller)
    id_string = b.Name

    Dim id() As String
    id = Split(id_string, "_")
    start_row = CInt(id(0))
    start_lvl = CInt(id(1))
    lvl_col = id(2)

'    MsgBox (lvl_col)
    Call hide_rows(start_lvl, start_row, lvl_col)
End Sub

Sub hide_rows(start_lvl, start_row, lvl_col)
    a = start_row + 1
    While Range(lvl_col & a) > start_lvl
    a = a + 1
    Wend

    If Range(lvl_col & start_row + 1, lvl_col & a - 1).EntireRow.Hidden = False Then
    Range(lvl_col & start_row + 1, lvl_col & a - 1).EntireRow.Hidden = True
    Else
    Range(lvl_col & start_row + 1, lvl_col & a - 1).EntireRow.Hidden = False
    End If


End Sub
Sub btnS_t()
    Dim but_r As Integer
    Set b = ActiveSheet.Buttons(Application.Caller)
    id_string = b.Name

    Dim id() As String
    id = Split(id_string, "_")
    start_row = CInt(id(0))
    start_lvl = CInt(id(1))
    lvl_col = id(2)
    b_toggle = CInt(id(3))

    If b_toggle = 0 Then
    b_toggle = 1
    Else
    b_toggle = 0
    End If

    b.Name = start_row & "_" & start_lvl & "_" & lvl_col & "_" & b_toggle

    Call hide_rows_tot(start_lvl, start_row, lvl_col, b_toggle)
End Sub
Sub hide_rows_tot(start_lvl, start_row, lvl_col, b_toggle)

    a = start_row

    While Range(lvl_col & a) <> ""
    b = a
    While Range(lvl_col & b) > start_lvl
    b = b + 1
    Wend

    If b > a Then
    If b_toggle = 1 Then
        Range(lvl_col & a, lvl_col & b - 1).EntireRow.Hidden = True
    Else
        Range(lvl_col & a, lvl_col & b - 1).EntireRow.Hidden = False
    End If

    a = b - 1
    End If
    a = a + 1
    Wend



End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文