我在microsoft Excel工作簿上编写VBA。我创建了一个5 CommandButtons的表单,我试图将这些CommandButtons分配给我创建的CommandButtons数组。我尝试通过将数组作为参数传递给函数来进行此分配,并在Workbook_Open事件中调用此函数,如下所示:
Dim Buttons(5) As CommandButton
Public Function InitializeButtons(ButtonArray() As CommandButton)
ButtonArray(0) = TableForm.CommandButton1
ButtonArray(1) = TableForm.CommandButton2
ButtonArray(2) = TableForm.CommandButton3
ButtonArray(3) = TableForm.CommandButton4
ButtonArray(4) = TableForm.CommandButton5
End Function
Private Sub Workbook_Open()
void = InitializeButtons(Buttons)
End Sub
问题是,由于某些原因,这个赋值永远不会执行,因为我得到以下错误消息:
运行时错误'91':对象变量或未设置块变量
如果你对如何解决这个问题有任何想法或建议,请告诉我。提前谢谢。
在分配对象变量时需要使用Set
Set ButtonArray(0) = TableForm.CommandButton1