2012-06-15 55 views
0

我需要创建一个Excel解决方案来记录关于家庭的数据。如何使用数据输入表格填写不同的Excel表格?

  1. 该窗体应该生成一个唯一的代码(autonumeric)来标识该记录。在这里,我要求提供姓名,父母的姓氏和出生日期。这记录在一张表中(称为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 

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

+0

你已经向我们展示了你为父母所尝试过的,它看起来可行,但是你为孩子尝试了什么?请向我们展示您尝试失败的方式,然后我们可以帮助您调整它。 –

回答

0

好的。对我来说慢一天。这里是你写出来的代码。我还清理了一些原始代码,以提高效率。

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