使用userform VBA将数据动态添加到行和列中

使用userform VBA将数据动态添加到行和列中,vba,excel,Vba,Excel,我是VBA Excel的新手。 我不知道如何将数据从userform动态插入到工作表或表格中,输出我想要的是当userform中的commandbutton单击时,然后将值插入为动态的参与者名称。这是我的代码: Private Sub CommandButton1_Click() Dim BarisSel As Long Sheets("db").Activate lRow = Application.WorksheetFunction.CountA(Range("

我是VBA Excel的新手。 我不知道如何将数据从userform动态插入到工作表或表格中,输出我想要的是当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中我想要的输出的屏幕截图:

我想:
您的代码将所有内容放在一行中。每次要在新行中插入内容时,必须将1添加到lRow,例如:

'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
...
您是否尝试使用查找代码偏离预期行为的地方

如果你不熟悉;您可以通过单击代码旁边的空白处来设置断点

当到达断点时,执行将停止

然后,您可以通过按F8一次执行一行代码。这是调试VBA的好方法,因为您可以看到每一行都在做什么。调试时,您可以将鼠标悬停在变量上,工具提示将显示当前值


如果调整VBA和Excel窗口的大小,使它们都可见,则可以在生成输出时看到输出。我怀疑您会发现更新lRow的代码没有如您预期的那样工作。

Hai目的地数据,谢谢您的回答。我经常使用即时窗口并按F8键来逐个了解我的代码正在运行。感谢您告诉我使用断点查找代码,这不是一个好建议…:-)你好,伊根·沃尔夫。就这样,我的问题解决了。谢谢你的回答。。。。。它似乎只是添加了一点代码。。。。非常感谢Egan.:-)