Excel VBA - 使用形状作为切换按钮

时间:2015-09-01 16:33:31

标签: excel excel-vba vba

我正在尝试使用形状而不是按钮来切换具有空白单元格的隐藏行(根据条件)。它甚至可能吗?

Sub ToggleChevron3_Click()
    Dim rng As Range, cell As Range
    Set rng = Range("A1:C100")

    Application.ScreenUpdating = False
        With rng
            For Each cell In rng                              
                    If cell.Offset(0, 4).Value = "" Then      ' Condition 1
                       If cell.Value = "" Then                ' Condition 2
                          ActiveSheet.Shapes("Chevron 3").cell.EntireRow.Hidden _
                         = Not ActiveSheet.Shapes("Chevron 3").cell.EntireRow.Hidden
                       End If
                    End If
            Next
        End With
    Application.ScreenUpdating = True
End Sub

1 个答案:

答案 0 :(得分:1)

是的,有可能。完成我认为你正在寻找的代码如下。下面的两段代码假设您只想单击一个按钮来隐藏/取消隐藏行,具体取决于当前状态。

dependencies {
    compile group: 'com.microsoft.sqlserver', name: 'sqljdbc4', version: '4.2'
}

但是,替代是更清晰的代码和更快的执行,只要过滤对你没用。

Sub ToggleChevron3_Click()

Application.ScreenUpdating = False

Dim rng As Range, cell As Range
'Set rng = Range("A1:C100") 'do you really want to loop through every cell in columns A through C
Set rng = Range("A1:A100")

For Each cell In rng

    If Len(cell.Offset(, 4).Value) = 0 And Len(cell.Value) = 0 Then

        Dim bToggle As Boolean
        bToggle = cell.EntireRow.Hidden

        cell.EntireRow.Hidden = Not bToggle

    End If

Next

Application.ScreenUpdating = True

End Sub