如何删除名称为 VBA 的 ActiveX 按钮?
How can I delete an ActiveX Button with VBA by its name?
我不明白为什么这段代码没有“检测”到 activex 按钮(在 Siddarth 的帮助下创建:)。我看到我面前的按钮及其属性 window 看起来像这样。
我什至尝试使用 ActiveSheet.Shapes("CommandButton1").Delete
仅删除一个按钮,但该行导致错误 window,并显示一条类似“未找到具有该名称的项目”的文本。 . => 显然我没有使用正确的名称来定位按钮。
我使用的代码基于 Word vba delete button not working
中使用的 Gareth 代码
我添加了一个 if 条件来排除名称设置为蛇形(因此为“_”)的“好”按钮,即。 e.这些名字是我定义的。
Sub del_button()
Dim obj As Object
For Each obj In ActiveSheet.Shapes
If InStr(obj.OLEFormat.Object.Name, "CommandButton") > 0 And InStr(obj.OLEFormat.Object.Name, "_") < 1 Then
'MsgBox (obj.OLEFormat.Object.Name) - just was there for me to "notice", so I am set to notice when a button is going to be deleted
obj.Delete
End If
Next obj
'ActiveSheet.Shapes("CommandButton1").Delete ---- THIS LINE throws an error
End Sub
=> 如果我使用的名称不正确,那么我应该在代码中指定什么名称?还是我必须完全使用其他东西?
这是有效的代码:
Sub del_button()
Dim obj As Object
For Each obj In ActiveSheet.Shapes
If InStr(obj.OLEFormat.Object.Name, "Object") > 0 And _
InStr(obj.OLEFormat.Object.Name, "_") < 1 Then
obj.Delete
End If
Next obj
End Sub
包含有用的细节,这些细节基本上是我问题的解决方案。
An ActiveX control on a sheet has two names: the name of the shape that contains the control, which you can see in the Name box when you view the sheet, and the code name for the control, which you can see in the cell to the right of (Name) in the Properties window. When you first add a control to a sheet, the shape name and code name match. However, if you change either the shape name or code name, the other isn't automatically changed to match.
You use the code name of a control in the names of its event procedures. However, when you return a control from the Shapes or OLEObjects collection for a sheet, you must use the shape name, not the code name, to refer to the control by name.
所以考虑到 2,
一些位:
名称,或者更确切地说,字符串,我必须检查的是形状名称。形状名称在 name box.
中可见
形状名称未本地化,但名称框中显示的文本已本地化。因此,上面 VBA 代码中的英文术语“Object”。
我不明白为什么这段代码没有“检测”到 activex 按钮(在 Siddarth 的帮助下创建:
我什至尝试使用 ActiveSheet.Shapes("CommandButton1").Delete
仅删除一个按钮,但该行导致错误 window,并显示一条类似“未找到具有该名称的项目”的文本。 . => 显然我没有使用正确的名称来定位按钮。
我使用的代码基于 Word vba delete button not working
中使用的 Gareth 代码我添加了一个 if 条件来排除名称设置为蛇形(因此为“_”)的“好”按钮,即。 e.这些名字是我定义的。
Sub del_button()
Dim obj As Object
For Each obj In ActiveSheet.Shapes
If InStr(obj.OLEFormat.Object.Name, "CommandButton") > 0 And InStr(obj.OLEFormat.Object.Name, "_") < 1 Then
'MsgBox (obj.OLEFormat.Object.Name) - just was there for me to "notice", so I am set to notice when a button is going to be deleted
obj.Delete
End If
Next obj
'ActiveSheet.Shapes("CommandButton1").Delete ---- THIS LINE throws an error
End Sub
=> 如果我使用的名称不正确,那么我应该在代码中指定什么名称?还是我必须完全使用其他东西?
这是有效的代码:
Sub del_button()
Dim obj As Object
For Each obj In ActiveSheet.Shapes
If InStr(obj.OLEFormat.Object.Name, "Object") > 0 And _
InStr(obj.OLEFormat.Object.Name, "_") < 1 Then
obj.Delete
End If
Next obj
End Sub
An ActiveX control on a sheet has two names: the name of the shape that contains the control, which you can see in the Name box when you view the sheet, and the code name for the control, which you can see in the cell to the right of (Name) in the Properties window. When you first add a control to a sheet, the shape name and code name match. However, if you change either the shape name or code name, the other isn't automatically changed to match.
You use the code name of a control in the names of its event procedures. However, when you return a control from the Shapes or OLEObjects collection for a sheet, you must use the shape name, not the code name, to refer to the control by name.
所以考虑到 2, 一些位:
名称,或者更确切地说,字符串,我必须检查的是形状名称。形状名称在 name box.
中可见形状名称未本地化,但名称框中显示的文本已本地化。因此,上面 VBA 代码中的英文术语“Object”。