2015-12-29 39 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来逐个知道我的代码正在运行。感谢您告诉我使用断点来查找代码,不是很好的建议... :-) –

相关问题