使用Togglebutton Excel 2016隐藏和取消隐藏包含日期的行

时间:2016-06-23 19:24:25

标签: vba togglebutton excel-2016

我尝试了大约20种不同的代码,尝试编辑它们以符合我的规格,但都失败了。

我有一张数据电子表格。一栏标题为“#34;完成"将有一个日期或没有日期(mm / dd / yyyy)。

我正在尝试编写一个代码,使用ToggleButton来隐藏和取消隐藏带有日期的行,如果没有日期则不管它。

1 个答案:

答案 0 :(得分:0)

请试一试。 假设你的日期在E栏。

Private Sub ToggleButton1_Click()
    Dim LastRow As Long, c As Range
    Application.EnableEvents = False
    LastRow = Cells(Cells.Rows.Count, "E").End(xlUp).Row
    If ToggleButton1.Value = True Then
        'This area contains the things you want to happen
        'when the toggle button is depressed
        For Each c In Range("E1:E" & LastRow)
    If c.Value = "" Then
        c.EntireRow.Hidden = True
    End If
    Next
    Else
    'This area contains the things you want to happen
    'when the toggle button is not depressed
        ActiveSheet.Range("E1:E" & LastRow).EntireRow.Hidden = False
    End If
End Sub

snapshot showing toggle action

编辑27-06-2016 略微修改程序以满足OP的要求。

Private Sub ToggleButton1_ClickRV()
    Dim LastRow As Long, c As Range
    Application.EnableEvents = False
    LastRow = Cells(Cells.Rows.Count, "E").End(xlUp).Row
    If ToggleButton1.Value = True Then
        'This area contains the things you want to happen
        'when the toggle button is depressed
        For Each c In Range("E1:E" & LastRow)
    If c.Value <> "" Then
        c.EntireRow.Hidden = False
    End If
    Next
    Else
    'This area contains the things you want to happen
    'when the toggle button is not depressed
        ActiveSheet.Range("E1:E" & LastRow).EntireRow.Hidden = True
    End If
End Sub