【问题标题】:Use VBA to review all CommandButtons使用 VBA 查看所有命令按钮
【发布时间】:2014-04-19 04:59:45
【问题描述】:

我知道这应该很简单....我有一个 Excel 电子表格,其中包含一些命令按钮(CommandButton1、CommandButton2、...)。我想在 for 语句中循环遍历这些按钮,而不使用 case 语句来确定要使用的按钮。我尝试了以下方法:

Dim cButtons(0 to 5) as CommandButton

cButtons(0) = Sheets("Sheet1").CommandButton1
cButtons(1) = Sheets("Sheet1").CommandButton2
'etc...

但这会给出错误“对象变量或未设置块变量”。任何帮助将不胜感激!

【问题讨论】:

  • 尝试使用Set关键字:Set cButtons(0) = Sheets("Sheet1").CommandButton1
  • 非常感谢!这是最简单的解决方案。作为 VBA 的新手,我不知道如何存储对对象的引用,例如 c++ 中的 int* a。
  • 只要记住每次你想将 object 分配给变量,使用Set:)

标签: vba excel


【解决方案1】:

为什么不忽略设置Object,如果它是一个循环,你可以这样做,除非你需要直接引用对象。

For i=1 To Sheet1.Shapes.Count
    Sheet1.Shapes.Item(i) 'Do Something with it
Next i

如果您确实需要直接引用,可以使用名称

Sheet1.Shapes.Item("CommandButton1") 'Do Something

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-28
    • 2012-03-18
    • 2021-10-11
    • 2015-06-11
    • 1970-01-01
    • 1970-01-01
    • 2015-10-19
    • 2012-03-25
    相关资源
    最近更新 更多