2015-12-29 86 views
1

我是VBA-Excel中的新手。 我有問題,如何從用戶表單插入數據爲動態的表格或表格,輸出我想要的是什麼時候用戶表單中的commandbutton點擊然後插入參數的動態名稱。這裏我的代碼:使用用戶窗體動態地將數據添加到行和列VBA

Private Sub CommandButton1_Click() 
Dim BarisSel As Long 
Sheets("db").Activate 
lRow = Application.WorksheetFunction.CountA(Range("A:A")) + 2 

Cells(lRow, 1) = cb_class.Text 
Cells(lRow, 2) = cb_room.Text 
Cells(lRow, 3) = tb_name.Text 

'insert caption for Training Filed and every Question 
Cells(lRow, 4) = trainingField.Caption 
Cells(lRow, 5) = Qustion_1.Caption 
Cells(lRow, 5) = Qustion_2.Caption 
Cells(lRow, 5) = Qustion_3.Caption 
Cells(lRow, 5) = Qustion_4.Caption 

'Answer Question number 1 using OptionButton 
If Jwb_1_A Then Cells(lRow, 6) = "A" 
If Jwb_1_B Then Cells(lRow, 6) = "B" 
If Jwb_1_C Then Cells(lRow, 6) = "C" 
'Remarks for Answer B or C using text box 
If Jwb_1_B.Value = True Then Cells(lRow, 7) = Tb_1_B.Text 
If Jwb_1_C.Value = True Then Cells(lRow, 7) = Tb_1_C.Text 


'Answer Question number 2 using OptionButton 
If Jwb_2_A Then Cells(lRow, 6) = "A" 
If Jwb_2_B Then Cells(lRow, 6) = "B" 
If Jwb_2_C Then Cells(lRow, 6) = "C" 
'Remarks for Answer B or C using text box 
If Jwb_2_B.Value = True Then Cells(lRow, 7) = Tb_2_B.Text 
If Jwb_2_C.Value = True Then Cells(lRow, 7) = Tb_2_C.Text 


'Answer Question number 3 using OptionButton 
If Jwb_3_A Then Cells(lRow, 6) = "A" 
If Jwb_3_B Then Cells(lRow, 6) = "B" 
If Jwb_3_C Then Cells(lRow, 6) = "C" 
'Remarks for Answer B or C using text box 
If Jwb_3_B.Value = True Then Cells(lRow, 7) = Tb_3_B.Text 
If Jwb_3_C.Value = True Then Cells(lRow, 7) = Tb_3_C.Text 


'Answer Question number 4 using OptionButton 
If Jwb_4_A Then Cells(lRow, 6) = "A" 
If Jwb_4_B Then Cells(lRow, 6) = "B" 
If Jwb_4_C Then Cells(lRow, 6) = "C" 
'Remarks for Answer B or C using text box 
If Jwb_4_B.Value = True Then Cells(lRow, 7) = Tb_4_B.Text 
If Jwb_4_C.Value = True Then Cells(lRow, 7) = Tb_4_C.Text 

.... 
.... 
.... 
.... 
.... 

'Until Question end 
End Sub 

輸出不是我想要的,只是當我改變下一個名稱參與者覆蓋。這是截圖在Excel輸出我想要的東西:

Output what i want

請幫助我。 在此先感謝。

回答

0

您的代碼將所有內容放在一行中。你必須要在一個新行插入一些每次,例如:

'insert caption for Training Field and every Question 
Range(Cells(lRow, 4), Cells(lRow + 3, 4)) = trainingField.Caption 
Cells(lRow, 5) = Qustion_1.Caption 
Cells(lRow + 1, 5) = Qustion_2.Caption 
Cells(lRow + 2, 5) = Qustion_3.Caption 
Cells(lRow + 3, 5) = Qustion_4.Caption 

'Answer Question number 1 using OptionButton 
If Jwb_1_A Then Cells(lRow, 6) = "A" 
If Jwb_1_B Then Cells(lRow, 6) = "B" 
If Jwb_1_C Then Cells(lRow, 6) = "C" 
'Remarks for Answer B or C using text box 
If Jwb_1_B.Value = True Then Cells(lRow, 7) = Tb_1_B.Text 
If Jwb_1_C.Value = True Then Cells(lRow, 7) = Tb_1_C.Text 


'Answer Question number 2 using OptionButton 
If Jwb_2_A Then Cells(lRow + 1, 6) = "A" 
If Jwb_2_B Then Cells(lRow + 1, 6) = "B" 
If Jwb_2_C Then Cells(lRow + 1, 6) = "C" 
'Remarks for Answer B or C using text box 
If Jwb_2_B.Value = True Then Cells(lRow + 1, 7) = Tb_2_B.Text 
If Jwb_2_C.Value = True Then Cells(lRow + 1, 7) = Tb_2_C.Text 
... 
+0

Hallo,Egan Wolf。就是這樣,我的問題已經完成了。感謝您的回答.....它似乎只是添加一點代碼....非常感謝Egan。 :-) –

1

您是否嘗試過使用breakpoints尋找到你的代碼從預期的行爲偏離加1 lRow?

如果你不熟悉;您可以通過單擊代碼旁邊的邊距來設置斷點。到達斷點時

enter image description here

執行將暫停。

enter image description here

然後,您可以在同一時間執行你的一行代碼,按F8。這是調試VBA的好方法,因爲您可以看到每行的功能。在調試時,您可以將鼠標懸停在變量上,工具提示會顯示當前值。

enter image description here

如果調整VBA和Excel窗口,因此兩者是可見的,你就可以看到輸出,因爲它是生成的。我懷疑你會發現更新lRow的代碼不能像你期望的那樣運行。

+0

海目的地 - 數據,謝謝你的回答。我經常使用即時窗口並按下F8來逐個知道我的代碼正在運行。感謝您告訴我使用斷點來查找代碼,不是很好的建議... :-) –

相關問題