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

我在VBA-Excel新手。 我有问题,如何将数据从用户表单插入到dynamic数据表或表格中,输出我想要的是什么时候用户表单中的commandbutton点击,然后将值作为参与者的dynamic名称插入。 在这里我的代码:

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中输出我想要的截图:

输出我想要的

请帮帮我。 提前致谢。

你的代码将所有内容放在一行中。 每次你想在新行插入某些东西时,你必须在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 ... 

您是否尝试过使用断点来查找代码偏离预期行为的位置?

如果你不熟悉, 您可以通过单击代码旁边的边距来设置断点。

在这里输入图像说明

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

在这里输入图像说明

然后,您可以按F8键一次执行一行代码。 这是debuggingVBA的好方法,因为您可以看到每一行正在做什么。 在debugging时,可以将鼠标hover在variables上,工具提示会显示当前值。

在这里输入图像说明

如果您调整VBA和Excel窗口的大小,以便两者都可见,则可以在生成输出时看到输出。 我怀疑你会发现更新lRow不能正常工作的代码。