2013-05-01 107 views
1

我是新來的excel VBA和我試圖適應一些堆棧溢出找到的解決方案,但我仍然卡住!VBA - 複製用戶從Combobox文本和粘貼循環到Excel工作表

我想在用戶窗體中的組合框中使用選定選項的文本,將此選定文本的行粘貼到Excel工作表中。

下面的代碼旨在選擇當前活動的用戶窗體的組合框值「CboIncomesPatch」並將該值粘貼到活動頁的單元格「M8」中,然後循環粘貼,直到計數器達到數字總數已達到。單位總數是一個文本框值「TxtNumberOfUnits」,例如「15」。組合框中的15個文本應粘貼在單元格「M8」中,然後粘貼所有後續行「M9」,「M10」等。ComboBox使用的範圍是硬編碼的,所有選項都是所有字符串的人員的名稱(沒有數字)。

錯誤發生在.SelectedItem下面的代碼上。當我使用它作爲文本框時,循環不起作用,不包括選擇代碼的組合框值部分。

請讓我知道,如果我可以提供更多的信息在這裏,以協助您的答案。

謝謝

尼爾

Sub Incomes_Patch() 

Dim Counter As Integer 
Dim ws As UserForm 
Dim Total As Integer 
Dim Incomes As String 

Set ws = UserForm(this.CboIncomesPatch.GetItemText(Me.CboIncomesPatch.SelectedItem)) 
Set Incomes.Value = ws 

Total = TxtNumberOfUnits.Value 
Counter = 0 

Application.ActiveSheet.range("M8").Select 
Do While Counter <= Total 
If Counter = Total Then Exit Do 
    ActiveCell.Value = Incomes + Counter 
    ActiveCell.Offset(1, 0).Select 
    Counter = Counter + 1 
Loop 

End Sub 

回答

0

請找到正確的代碼來做到這一點如下:

Sub Incomes_Patch() 

Dim Counter As Integer 
'Dim ws As UserForm 
Dim Total As Integer 
Dim Incomes As String 

'I am not sure why you wanted the form object, you need the combobox value right? you can directly get it from Combobox. 

'Set ws = UserForm(this.CboIncomesPatch.GetItemText(Me.CboIncomesPatch.SelectedItem)) 
Incomes = Me.CboIncomesPatch.Text ' selected item doesn't work in Combo, so you need to use Text 

Total = TxtNumberOfUnits.Text 
Counter = 0 

Application.ActiveSheet.Range("M8").Select 
Do While Counter <= Total 
If Counter = Total Then Exit Do 
    ActiveCell.Value = Incomes & Counter 
    ActiveCell.Offset(1, 0).Select 
    Counter = Counter + 1 
Loop 

End Sub 

幫助?

謝謝,V

+0

是V的工作。非常感謝你。 – Neil 2013-05-02 10:42:01

+0

對不起 - 剛接受它。沒有意識到我需要這樣做。謝謝你,尼爾 – Neil 2013-05-03 09:03:02

0

難道你不使用組合框的.SelectedText財產?

Set ws = UserForm(Me.CboIncomesPatch.SelectedText) 
Set Incomes.Value = ws