禁用Excel工作表中的所有button

我想在Excel 2013中单击它们后禁用button。

我的代码工作正常,但只是一个特定的button。

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

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

我想这是你要找的东西:

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

从您的问题中不清楚您是否要按Button1来“禁用”工作表上的所有控件,或者是否希望每个button都禁用它自己。

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 

每个button禁用自己使用通用buttondisabler帮手程序…

 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 

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

 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