VBAvariables作为CommandButton#

我重写了一些代码,并有一个想法,但似乎无法让我的语法正确执行它的权利。 我想使用for循环来填充一个commandbuttons数组,并控制其可见性。 我只需要帮助我的语法来定义我在循环中工作的CommandButton编号。 例如,CommandButton1,CommandButton2等

Public Sub LoadLots(sName As String, streamLots() As String) Label1.Caption = sName For o = 1 To 9 If streamLots(o) <> "" Then CommandButton& o &.Caption = streamLots(o) CommandButton& o & .Visable = True Else CommandButton& o & .Visable = False End If Next End Sub 

使用Userform.Controls集合按名称引用命令button。

 Public Sub LoadLots(sName As String, streamLots() As String) Dim btn As MSForms.CommandButton Label1.Caption = sName For o = 1 To 9 Set btn = Me.Controls("CommandButton" & o) If streamLots(o) <> "" Then btn.Caption = streamLots(o) btn.Visible = True Else btn.Visible = False End If Next End Sub