简体   繁体   English

使用相同的用户表单 VBA Excel 填充多个表

[英]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.

相关问题 Excel VBA代码提交多个用户表单数据以填充工作表 - Excel VBA code to submit multiple userform data to populate worksheet 使用 VBA 格式化 Excel 用户窗体中的多个控件 - Formatting Multiple Controls in an Excel UserForm using VBA 同一用户窗体上的多个文本框的相同宏 - Same macro for multiple textboxes on the same userform excel vba 使用用户窗体在 Excel 中填充行 - Using a UserForm to Populate Rows in Excel VBA Excel 用户窗体组合框填充/添加 - VBA Excel UserForm ComboBox Populate/Add 使用Excel 2010在同一用户窗体上的组合框中输入数字后,如何使用vlookup填充用户窗体上的列表框 - how to use vlookup to populate a listbox on a userform after inputting a number in a combobox on the same userform using excel 2010 Excel VBA用户表单-使用相同的表单生成连续数据 - Excel VBA userform - using the same form to generate continuous data 使用VBA在Excel用户窗体中选择由选项指示的多个下拉框 - Selecting multiple dropdown boxes indicated by options in an Excel UserForm using VBA 在运行时使用vba将多个标签和文本框添加到Excel用户表单 - adding multiple labels and textboxes to an Excel userform during runtime using vba Excel VBA 用户表单使用复选框将数据输入多行 - Excel VBA Userform Entering Data into Multiple rows using checkboxes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM