[英]Populate multiple tables using same userform VBA Excel
I am using following code to transfer data from user forms (with similar question set) using command buttons to populate different cells in 15-25 separate small tables in a same worksheet as per project requirement.我正在使用以下代码从用户表单(具有类似的问题集)传输数据,使用命令按钮根据项目要求在同一工作表中的 15-25 个单独的小表中填充不同的单元格。
As I am new to VBA coding, my approach is to repeat the following code for each table to populate cells but looking for efficient way to perform this action as all user forms with questions are same but expecting different responses for each table.由于我是 VBA 编码的新手,我的方法是为每个表重复以下代码以填充单元格,但正在寻找执行此操作的有效方法,因为所有带有问题的用户表单都相同,但期望每个表有不同的响应。
Example: Each worksheet has 15-25 tables where we need to populate each table with personal information (such as Name, age, gender, date of birth, school, city) of group of students using same user forms with same question set.示例:每个工作表有 15-25 个表,我们需要使用相同的用户表单和相同的问题集在每个表中填充一组学生的个人信息(例如姓名、年龄、性别、出生日期、学校、城市)。
I need to know if there is a better way to write this code once only (as user forms and questions are all same) for all tables in same worksheet and populate data in different tables using table-specific command buttons.我需要知道是否有更好的方法可以为同一工作表中的所有表仅编写一次此代码(因为用户表单和问题都相同),并使用特定于表的命令按钮在不同表中填充数据。
My current code and approach is:我目前的代码和方法是:
Private Sub SubmitButtonForm1_Click()
wsWorkPlan.Select
Range("B13").Select
ActiveCell.End(xlDown).Select
lastRow = ActiveCell.Row
Cells(lastRow + 1, 6).Value = txtQ1.Text
Cells(lastRow + 1, 7).Value = txtQ2.Text
Cells(lastRow + 1, 2).Value = txtQ3.Text
Cells(lastRow + 1, 8).Value = cmbQ4.Text
Cells(lastRow + 1, 5).Value = cmbQ5.Text
Cells(lastRow + 1, 4).Value = cmbQ6.Text
End Sub
Private Sub SubmitButtonForm2_Click()
wsWorkPlan.Select
Range("B55").Select
ActiveCell.End(xlDown).Select
lastRow = ActiveCell.Row
Cells(lastRow + 1, 6).Value = txtQ1.Text
Cells(lastRow + 1, 7).Value = txtQ2.Text
Cells(lastRow + 1, 2).Value = txtQ3.Text
Cells(lastRow + 1, 8).Value = cmbQ4.Text
Cells(lastRow + 1, 5).Value = cmbQ5.Text
Cells(lastRow + 1, 4).Value = cmbQ6.Text
End Sub
Private Sub SubmitButtonForm3_Click()
wsWorkPlan.Select
Range("B76").Select
ActiveCell.End(xlDown).Select
lastRow = ActiveCell.Row
Cells(lastRow + 1, 6).Value = txtQ1.Text
Cells(lastRow + 1, 7).Value = txtQ2.Text
Cells(lastRow + 1, 2).Value = txtQ3.Text
Cells(lastRow + 1, 8).Value = cmbQ4.Text
Cells(lastRow + 1, 5).Value = cmbQ5.Text
Cells(lastRow + 1, 4).Value = cmbQ6.Text
End Sub
Many thanks in advance for your kind help!非常感谢您的帮助!
Consider a separate function behind userform or standard module that you have each button call passing needed parameters like cell range:考虑用户窗体或标准模块背后的一个单独函数,您让每个按钮调用传递所需的参数,如单元格范围:
Private Sub SubmitButtonForm1_Click()
Call UpdateCells("B13")
End Sub
Private Sub SubmitButtonForm2_Click()
Call UpdateCells("B55")
End Sub
Private Sub SubmitButtonForm3_Click()
Call UpdateCells("B76")
End Sub
Public Function UpdateCells(strCell As String)
wsWorkPlan.Select
Range(strCell).Select
ActiveCell.End(xlDown).Select
lastRow = ActiveCell.Row
Cells(lastRow + 1, 6).Value = txtQ1.Text
Cells(lastRow + 1, 7).Value = txtQ2.Text
Cells(lastRow + 1, 2).Value = txtQ3.Text
Cells(lastRow + 1, 8).Value = cmbQ4.Text
Cells(lastRow + 1, 5).Value = cmbQ5.Text
Cells(lastRow + 1, 4).Value = cmbQ6.Text
End Function
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.