【发布时间】: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:)