2015-11-02 97 views
1

嗨,我想創建一定數量的組合框時按下一個commandbutton。我不知道該怎麼做,所以我會非常感謝你的幫助。這是創建的代碼:在循環VBA中創建組合框

Private Sub CommandButton1_Click() 
    Dim AttPoints As Integer, Result As String 
    Range("E1:Z4").ClearContents 
    AttPoints = Range("B2").Value 

    If AttPoints = 0 Then 
     Result = "You have selected 0 AttPoints!" 

    ElseIf AttPoints < 0 Then 
     Result = "You have selected a negative amount of AttPoints!" 

    ElseIf AttPoints > 0 Then 
     Dim i As Integer 
     For i = 5 To (AttPoints + 4) 
     Cells(1, i).Value = "Attachment point:" & (i - 4) 
     Next i 

    End If 
    Range("A1") = Result 
End Sub 

在for循環中,我創建了一行單元格,其中放置了文本附加點。 在這些文本下,我想要在圖片中看到相同數量的組合框。

enter image description here

回答

1

添加以下代碼位的循環

Private Sub CommandButton1_Click() 

    ... 

    Shapes.AddOLEObject ClassType:="Forms.Combobox.1", _ 
    Left:=Cells(2, i).Left, Top:=Cells(2, i).Top, _ 
    Width:=Cells(2, i).Width, Height:=Cells(2, i).Height * 2 

    ... 

End Sub 

這應該出示你想要的結果裏。

+0

謝謝!我還應該包含Private Sub CommandButton1_Click()部分? –

+0

不,你只需要在你的循環中顯然包含3條線。如果問題解決了,請接受上面的答案作爲解決方案。 – Gess