使用形状作为宏的按钮来隐藏/取消隐藏行

时间:2018-08-02 17:06:52

标签: excel vba

我正在使用以下内容来隐藏和取消隐藏某些行,但是我想使用形状-“ RectangleRoundedCorners9”-代替难看的按钮。该脚本可以在一个按钮上很好地工作(完全可以实现我想要的功能),但是只能在一个实际的按钮上工作。

我不了解VBA,也不确定如何使此代码与该形状(而不是按钮)一起使用:

Private Sub ToggleButton1_Click()
Dim xAddress As String
xAddress = "F:G"
If ToggleButton1.Value Then
    Application.ActiveSheet.Columns(xAddress).Hidden = True
Else
    Application.ActiveSheet.Columns(xAddress).Hidden = False
End If
End Sub

我尝试如下替换,但是在IF行上收到424“找不到对象”错误:

Private Sub RectangleRoundedCorners9_Click()
Dim xAddress As String
xAddress = "F:G"
If RectangleRoundedCorners9.Value Then
    Application.ActiveSheet.Columns(xAddress).Hidden = True
Else
    Application.ActiveSheet.Columns(xAddress).Hidden = False
End If
End Sub

谢谢。

奖金:我想将最终产品注入以下内容,以使形状也像按钮一样具有视觉效果:

Sub SimulateButtonClick()
Dim vTopType As Variant
Dim iTopInset As Integer
Dim iTopDepth As Integer


    With ActiveSheet.Shapes(Application.Caller).ThreeD
        vTopType = .BevelTopType
        iTopInset = .BevelTopInset
        iTopDepth = .BevelTopDepth
    End With


    With ActiveSheet.Shapes(Application.Caller).ThreeD
        .BevelTopType = msoBevelSoftRound
        .BevelTopInset = 12
        .BevelTopDepth = 4
    End With
    Application.ScreenUpdating = True


    With ActiveSheet.Shapes(Application.Caller).ThreeD
        .BevelTopType = vTopType
        .BevelTopInset = iTopInset
        .BevelTopDepth = iTopDepth
    End With

'---------------
'HIDE/UNHIDE SCRIPT HERE
'---------------
End Sub

1 个答案:

答案 0 :(得分:0)

类似这样的东西:

Sub ToggleCols()

    Const RNG As String = "F:G"
    Dim s, tr

    Set s = ActiveSheet.Shapes(Application.Caller)
    Set tr = s.TextFrame2.TextRange

    ActiveSheet.Columns(RNG).Hidden = (tr.Text = "Hide")
    tr.Text = IIf(tr.Text = "Hide", "Show", "Hide")

End Sub