我在VBA-Excel中的新手。 我有问题如何将数据作为动态从userform插入到工作表或表中,输出我想要的是当用户窗体中的commandbutton点击然后值插入作为参与者的动态基础名称。在这里我的代码:
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中的截图,用于输出我想要的内容:
答案 0 :(得分:1)
您是否尝试使用breakpoints查找代码偏离预期行为的位置?
如果你不熟悉;您可以通过单击代码旁边的边距来设置断点。
达到断点时,执行将暂停。
然后按F8,您可以一次执行一行代码。这是调试VBA的好方法,因为您可以看到每条线路正在做什么。在调试时,您可以将鼠标悬停在变量上,工具提示将显示当前值。
如果您调整VBA和Excel窗口的大小,以便两者都可见,您将能够在生成时看到输出。我怀疑你会发现更新lRow的代码没有按预期运行。
答案 1 :(得分:0)
您的代码将所有内容放在一行中。每次要在新行中插入内容时,必须向lRow添加1,例如:
'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
...