首页 > 解决方案 > 使用 CommandButton Collection 更新命令按钮上的字体

问题描述

我想使用 vba 更改几个工作表上所有 ActiveX 命令按钮的字体。我知道可以使用 vba 更新字体,因为此代码将为单个按钮执行此操作

Private Sub CommandButton49_Click()
CommandButton49.Font = "Arial Rounded MT Bold"
End Sub

我在这个网站上看到了一种获取收藏的方法

Dim MyShapes As OLEObjects
Dim btn As OLEObject
Dim cbtn As CommandButton

'OLE Programmatic Identifiers for Commandbuttons = Forms.CommandButton.1
Set MyShapes = ActiveSheet.OLEObjects
For Each btn In MyShapes
   If btn.progID = "Forms.CommandButton.1" Then
      btn.Object.Font = "Arial"
   End If
Next

但是这段代码给出了一个运行时错误 438 - 对象不支持这个属性或方法,这很奇怪,因为我可以使用这个方法来显示字体 - 即 msgbox btn.Object.Font 将显示当前字体。

我错过了什么?

标签: excelvba

解决方案


推荐阅读