【问题标题】:How to fill different excel sheets using data entry forms?如何使用数据输入表格填写不同的 Excel 表格?
【发布时间】:2012-09-11 20:49:10
【问题描述】:

我需要创建一个 Excel 解决方案来记录有关家庭的数据。

  1. 表单应生成唯一代码(自动数字)来识别该记录。在这里,我询问父母的姓名、姓氏和出生日期。这会记录在一张表中(称为Parents)。
  2. 一个家庭可以包含多个孩子,所以我认为这必须记录在另一张纸上(称为Children),以保持与他们父母的关系。

我能够将每个不同的字段保存到其对应的单元格(映射)。这是我用来获取它的代码:

Private Sub cmdSave_Click()
ActiveWorkbook.Sheets("Parents").Activate
Range("B2").Select
Do
If IsEmpty(ActiveCell) = False Then
    ActiveCell.Offset(1, 0).Select
End If
Loop Until IsEmpty(ActiveCell) = True

'Father
ActiveCell.Value = txtFatherFN.Text
ActiveCell.Offset(0, 1) = txtFatherSN.Text
ActiveCell.Offset(0, 2) = txtFatherLN.Text
ActiveCell.Offset(0, 3) = txtFatherBirthDay.Text
'Mother
ActiveCell.Offset(0, 4) = txtMotherFN.Text
ActiveCell.Offset(0, 5) = txtMotherSN.Text
ActiveCell.Offset(0, 6) = txtMotherLN.Text
ActiveCell.Offset(0, 7) = txtMotherBirthDay.Text
Range("B2").Select
End Sub

所以,我需要一种方法来生成代码并保持与孩子的关系。 欢迎任何帮助!

【问题讨论】:

  • 您已经向我们展示了您为父母所做的尝试,并且看起来很有效,但是您为孩子尝试了什么?请向我们展示您的尝试和失败之处,我们可以帮助调整它。

标签: vba excel


【解决方案1】:

好的。对我来说缓慢的一天。这是为您编写的代码。我还稍微清理了您的原始代码以提高效率。

Option Explicit

Private Sub cmdSave_Click()

Dim wks As Worksheet

Set wks = Sheets("Parents")

'assumes headers in First Row, ID is Column A
With wks

    'find next empty row
    Dim lngRow As Long
    lngRow = .Cells(Rows.Count, 1).End(xlUp).Offset(1).Row

    'find next unique Id
    Dim lngID As Long
    lngID = Application.WorksheetFunction.Max("A:A") + 1

    'id
    .Cells(lngRow, 1) = lngID
    'Father
    .Cells(lngRow, 2) = txtFatherFN.Text
    .Cells(lngRow, 3) = txtFatherSN.Text
    .Cells(lngRow, 4) = txtFatherLN.Text
    .Cells(lngRow, 5) = txtFatherBirthDay.Text
    'Mother
    .Cells(lngRow, 6) = txtFatherFN.Text
    .Cells(lngRow, 7) = txtFatherSN.Text
    .Cells(lngRow, 8) = txtFatherLN.Text
    .Cells(lngRow, 9) = txtFatherBirthDay.Text
End With

Set wks = Sheets("Children")

With wks

    'find next empty row
    lngRow = .Cells(Rows.Count, 1).End(xlUp).Offset(1).Row

    'need to adjust the for statement for however you collect your children in your form
    For Each Child In Children 'this is an example only to illustrate. You will need to change Child / Children to the objects used to collect the children info.
        .Cells(lngRow, 1) = Child
        lngRow = lngRow + 1
    Next

End With


End Sub

【讨论】:

    猜你喜欢
    • 2018-08-13
    • 2017-11-07
    • 2020-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多