2016-07-24 37 views
1

我重寫了一些代碼,並有一個想法,但似乎無法讓我的語法正確執行它。我想使用for循環來填充一個commandbuttons數組並控制它們的可見性。我只需要我的語法幫助來定義我在循環中處理哪個CommandButton編號。例如,CommandButton1,CommandButton2等。VBA變量作爲CommandButton#

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 
+2

'Activesheet.Shapes( 「命令按鈕」 &O)... .Caption'也'Visable'應該是'Visible' –

+0

我應該補充,這些按鈕在窗體上。 – Flibertyjibbet

+0

原理相同,但下面也有答案。 –

回答

2

使用Userform.Controls集合按名稱引用commandbutton。

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 
+0

完美,謝謝。 – Flibertyjibbet

+0

很高興我能夠幫助。感謝您的支票。 – 2016-07-24 23:03:23