我相信会有几种方法来解决这个问题。我的第一个想法是添加复选框,将它们全部链接到一个宏。激活后,您必须做几件事:
找出谁在调用子(哪个复选框);
找出该特定复选框的位置(哪一行);
隐藏/取消隐藏它下面的行。
1:
复选框的名称很简单。Application Caller
会给你的。
2:
位置是这里真正的问题。我在这里看不到一个简单的解决方案,除了给复选框提供这样的特定名称之外,它在哪一行很清楚。如果添加一个复选框,您可以在“命名范围”输入字段中给出名称。如果你给它命名来指定它必须隐藏的行,那就更好了。所以类似于:
HIDE_4_7
表示复选框必须隐藏/取消隐藏第 4 到 7 行。
3:
隐藏行现在很容易。
整体解决方案:
Sub HideRows()
Dim cbName As String
Dim cbValue As Boolean
Dim s() As String
Dim firstRow As Long
Dim lastRow As Long
On Error Resume Next
cbName = Application.Caller
If Err.Number <> 0 Then Exit Sub 'sub is not called from an application object
cbValue = (ActiveSheet.CheckBoxes(cbName) = xlOn)
If Err.Number <> 0 Then Exit Sub 'sub is not called from a checkbox
On Error GoTo 0
s = Split(cbName, "_")
If s(LBound(s)) <> "HIDE" Then Exit Sub 'name of the shape is not valid
firstRow = Val(s(LBound(s) + 1))
lastRow = Val(s(LBound(s) + 2))
Sheets(1).Rows(firstRow & ":" & lastRow).Hidden = Not cbValue
End Sub
您必须调用复选框HIDE_*firstrow*_*lastrow*
,并将它们链接到此子。这对我有用。
编辑
要在打开时隐藏所有行,您可以使用 Workbook_Open 子(在工作簿代码存储中)。像这样的东西:
Private Sub Workbook_Open()
Dim shp As Shape
Dim s() As String
Dim firstRow As Long
Dim lastRow As Long
Dim cbValue As Boolean
For Each shp In Sheets(1).Shapes
Debug.Print shp.Name
s = Split(shp.Name, "_")
If s(LBound(s)) <> "HIDE" Then GoTo nextShp
'set checkbox off:
Sheets(1).CheckBoxes(shp.Name) = xlOff
firstRow = Val(s(LBound(s) + 1))
lastRow = Val(s(LBound(s) + 2))
Sheets(1).Rows(firstRow & ":" & lastRow).Hidden = True
nextShp:
Next shp
End Sub