切换显示/隐藏表

时间:2018-08-14 13:53:32

标签: vba excel-vba

我想隐藏一个工作表,仅在双击特定工作表时才显示它。 假设我有sheet1和sheet2。我希望Sheet2一直处于隐藏状态,直到我双击Sheet1中的一个单元格(在Sheet1上的任何地方而不是特定的单元格)并在我离开它时再次将其隐藏。可能吗? 我读过一些有关

Private Sub Workbook_SheetBeforeDoubleClick(ByVal Sh As Object,ByVal Target As Range, ByVal Cancel As Boolean)

但是我无法使用它。 谢谢您的时间和帮助

3 个答案:

答案 0 :(得分:1)

也许这一切都在sheet1模块中(右键单击工作表标签,查看代码和粘贴代码)

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)

Cancel = True

Sheet2.Visible = xlSheetVisible

End Sub

Private Sub Worksheet_Deactivate()

Sheet2.Visible = xlSheetHidden

End Sub

答案 1 :(得分:1)

将其放置在Sheet1代码窗格中:

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    With Worksheets("Sheet2")
        .Visible = True
        .Activate
    End With
End Sub

并将其放置在sheet2代码窗格中:

Private Sub Worksheet_Deactivate()
    Me.Visible = False
End Sub

答案 2 :(得分:0)

我在Windows 7下使用Excel 2010测试了以下内容... ...将以下内容粘贴到“ ThisWorkbook”代码窗格中...希望这会有所帮助...

Dim strLastActive As String
Private Sub Worksheet_Activate()
    strLastActive = ActiveSheet.Name
End Sub
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    If ActiveSheet Is ThisWorkbook.Worksheets("Sheet1") Then
        If Not ThisWorkbook.Worksheets("Sheet2").Visible Then
            ThisWorkbook.Worksheets("Sheet2").Visible = True
        End If
    End If
End Sub
Private Sub Worksheet_Deactivate()
    If strLastActive = "Sheet2" Then
        ThisWorkbook.Worksheets("Sheet2").Visible = False
    End If
End Sub