2016-07-24 5 views
1

コードを書き直していますが、それを正しく実行するための構文が正しくないようです。私はforループを使用して、コマンドボタンの配列を生成し、その可視性を制御したいと考えています。私はループ内で作業しているCommandButtonの番号を定義するために、構文に助けが必要です。たとえば、CommandButton1、CommandButton2などVBA Variable as 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コレクションを使用します。

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

私は助けてくれてうれしいです。チェックしていただきありがとうございます。 –