2012-06-15 56 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