人气 3228

Excel实现分级菜单-多级展开 [复制链接]

wangjs 2016-11-16 22:31:05
使用Treeview树控件来实现分级菜单-多级展开,您可能经常见到,但Excel自身使用单元格来实现分级菜单可能您很少见吧下面就使用VBA代码来实现这个功能。
Excel中国源创教程:
实现的效果图如下:

1479306486103672.png

1479306486103672.png

动画展示:

1479306524878708.gif

1479306524878708.gif

实现的VBA完整代码:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)  Dim k, i, j, p  If Target.Row = 1 Or Target.Text = "" Or Target.Column > 2 Then Exit Sub  Application.ScreenUpdating = False  j = UsedRange.Rows.Count  If Target.NumberFormatLocal = "@* -" Then    Target.NumberFormatLocal = "@* +"    k = True  Else    Target.NumberFormatLocal = "@* -"    k = False  End If  For i = Target.Row + 1 To j    Rows(i).EntireRow.Hidden = k    If Target.Column = 1 Then      If Cells(i, 2).NumberFormatLocal = "@* +" Or k = True Then        p = True      ElseIf Cells(i, 2).NumberFormatLocal = "@* -" Then        p = False      End If      If Cells(i, 2) = "" Then Rows(i).EntireRow.Hidden = p    End If    If Cells(i + 1, 1)  "" Or Target.Column = 2 And Cells(i + 1, 2)  "" Then Exit For  Next i  Cancel = True  Application.ScreenUpdating = TrueEnd Sub
您需要登录后才可以回帖 登录 | 立即注册

QQ|手机版|精益人 ( 沪ICP备19004111号-1 )|网站地图

GMT+8, 2024-12-22 19:17 , Processed in 0.319894 second(s), 23 queries .

Powered by Lean.ren X3.5 Licensed  © 2001-2030 LEAN.REN