2016-01-21 361 views

回答

0

假設你的意思是你想提取物從一個ListBox數據到特定的細胞

,這裏是我的答案

用戶窗體的代碼中放一個按鈕功能

Private Sub CommandButton1_Click() 
    Dim i 'to store the item of the list 
    Dim j 'Just a counter 
    j = 0 'Ini the counter 
    For Each i In Me.ListBox1.List 'Ini the for loop 
     j = j + 1 'Add one to the counter 
     Cells(j, 4).Value = i 'here is the thing. 
     'In the cell(row = j, column = 4 = D) put the value: i = the item of the list 
     'This take all the items of the list, inside the combobox. 
    Next i 'Next one please! 
End Sub 

編輯#1

閱讀(老化)你的問題,看到你想寫一個不活動的表中的列表......好吧,在這裏!

Private Sub CommandButton1_Click() 
    Dim i 'to store the item of the list 
    Dim j 'Just a counter 
    Dim sht As Worksheet 'the var for the sheet you want 
    Set sht = Sheets("S") 
    'the name of the sheet I want is "S", stored inside the var sht 
    j = 0 'Ini the counter 
    For Each i In Me.ListBox1.List 'Ini the for loop 
     j = j + 1 'Add one to the counter 
     sht.Cells(j, 4).Value = i 'here is the thing. 
     'In the cell(row = j, column = 4 = D) 
     'put the value: i = the item of the list 
     'This take all the items of the list, inside the combobox. 
    Next i 'Next one please! 
End Sub 

編輯#2

現在你可以在ListBox中使用它。而且這種改變非常簡單,只需擦除ComboBox1並編寫「ListBox1」,它就可以與這兩個控件一起使用。

編輯#3

有了這個代碼,你把所有的單個細胞

Private Sub CommandButton1_Click() 
    Dim i 'to store the item of the list 
    Dim j 'Just a counter 
    Dim sht As Worksheet 'the var for the sheet you want 
    Set sht = Sheets("S") 
    'the name of the sheet I want is "S", stored inside the var sht 
    j = 0 'Ini the counter 
    For Each i In Me.ListBox1.List 'Ini the for loop 
     j = j + 1 'Add one to the counter 
     sht.Cells(1, 4).Value = sht.Cells(1, 4).Value & Chr(10) & i 
     'here is the thing. 
     'In the cell(row = 1, column = 4 = D) 
     'put the value: i = the item of the list 
     'besides the others values. 
     'Everything inside a single cell 
     'This take all the items of the list, inside the combobox. 
    Next i 'Next one please! 
End Sub 

裏面,如果你需要一些改進告訴我。

+0

它是一個列表框,你知道該怎麼做嗎? –

+0

檢查我的編輯#2,我做了更改,並注意該代碼適用於組合框和列表框。 –

+0

而且變化非常簡單,只需查看兩個控件的屬性就可以瞭解需要進行哪些更改。如果你想要更多有關這些控件的文檔,你可以看看這些控件可以在VBA中完成。或者只是寫下控件的名稱,然後指向可用屬性的列表。 –

相關問題