禁用Excel工作表中的所有按钮

时间:2016-09-13 10:59:20

标签: excel vba

我希望在Excel 2013中单击按钮后禁用按钮。

我的代码工作正常但只有一个特定的按钮。

如何将相同的逻辑应用于工作表中的所有按钮?

ActiveSheet.Shapes("Button 1").OnAction = Empty
ActiveSheet.Shapes("Button 1").DrawingObject.Font.ColorIndex = 16

3 个答案:

答案 0 :(得分:2)

从您的问题中不清楚您是否要按下Button1到"禁用"工作表上的所有控件,或者您是否希望每个按钮自行禁用。

Button1禁用所有控件

Sub Button1_Click
  Dim shp As Shape
  For Each shp In Sheet1.Shapes
    With shp
      If .Type = msoFormControl Then
        .OnAction = ""
        .DrawingObject.Font.ColorIndex = 16
      End If
    End With
  Next shp
End Sub

每个按钮自行禁用 使用常用按钮禁用辅助程序...

Sub Button1_Click()
  DisableButton Sheet1, "Button 1"
End Sub

Sub Button2_Click()
  DisableButton Sheet1, "Button 2"
End Sub

Sub DisableButton(hostSheet As Worksheet, shapeName As String)
  Dim shp As Shape
  On Error Resume Next
  Set shp = hostSheet.Shapes(shapeName)
  On Error GoTo 0
  If Not shp Is Nothing Then
    With shp
      If .Type = msoFormControl Then
        .OnAction = ""
        .DrawingObject.Font.ColorIndex = 16
      End If
    End With
  End If
End Sub

答案 1 :(得分:0)

我猜这是你正在寻找的东西:

Sub Answer()
dim sh as shape
For Each Sh In ActiveSheet.Shapes
    Sh.OnAction = Empty
    Sh.DrawingObject.Font.ColorIndex = 16
Next
End Sub

答案 2 :(得分:0)

这应该隐藏工作表中的所有表单控件(包括按钮)。

Dim ws_export As Worksheet 
Dim shp_hide As Shape

 For Each shp_hide In ws_export.Shapes
    If shp_hide.Type = msoFormControl Then shp_hide.Visible = FALSE

 Next shp_hide