我有这个程序,我必须计算BMI。 除了一件事,当我按下" New Entry"按钮向电子表格添加条目,用户窗体消失但用户窗体的窗口框架没有。
请参阅附图:
这是" New Entry"的代码。按钮:
Private Sub newEntryButton_Click()
Dim lastName As String
Dim firstName As String
Dim middleName As String
Dim birthYear As Integer
lastName = lastNameText.Text
firstName = firstNameText.Text
middleName = middleNameText.Text
birthYear = birthCmbx.Value
weight = weightText.Text
height = heightText.Value
If maleOpt.Value = True Then
maleTab.Activate
Sheets("maleTab").Cells(x, 1) = lastName 'last name'
Sheets("maleTab").Cells(x, 2) = firstName 'first name'
Sheets("maleTab").Cells(x, 3) = middleName 'middle name'
Sheets("maleTab").Cells(x, 4) = birthCmbx.Value 'birth year'
Sheets("maleTab").Cells(x, 5) = ageDiff(birthYear) 'age'
Sheets("maleTab").Cells(x, 6) = weight 'weight'
Sheets("maleTab").Cells(x, 7) = height 'height'
Sheets("maleTab").Cells(x, 8) = bmiFactorNum.Text
Sheets("maleTab").Cells(x, 9) = bmiFactorText.Text
x = x + 1
ElseIf femaleOpt.Value = True Then
femaleTab.Activate
Sheets("femaleTab").Cells(x2, 1) = lastName
Sheets("femaleTab").Cells(x2, 2) = firstName
Sheets("femaleTab").Cells(x2, 3) = middleName
Sheets("femaleTab").Cells(x2, 4) = birthCmbx.Value
Sheets("femaleTab").Cells(x2, 5) = ageDiff(birthYear)
Sheets("femaleTab").Cells(x2, 6) = weight
Sheets("femaleTab").Cells(x2, 7) = height
Sheets("femaleTab").Cells(x2, 8) = bmiFactorNum.Text
Sheets("femaleTab").Cells(x2, 9) = bmiFactorText.Text
x2 = x2 + 1
Else
MsgBox "Please select a gender"
End If
End Sub
答案 0 :(得分:2)
也许所有Activate
都在造成任何伤害......
或者有一些事件处理在每个单元格写入时被触发
尝试使用此代码
Option Explicit
Private Sub newEntryButton_Click()
Dim targetSht As Worksheet
Select Case True
Case maleOpt.Value
Set targetSht = Worksheets("maleTab")
Case femaleOpt.Value
Set targetSht = Worksheets("femaleTab")
Case Else
MsgBox "Please select a gender"
Exit Sub
End Select
Application.EnableEvents = False ' disable events
With Me 'reference your userform
'write in targetSht 9 columns-1 row range starting from column A first empty cell after last not empty one
targetSht.Cells(targetSht.Rows.Count, 1).End(xlUp).Offset(1).Resize(, 9) = Array( _
.lastNameText.Text, _
.firstNameText.Text, _
.middleNameText.Text, _
.birthCmbx.Value, _
ageDiff(.birthCmbx.Value), _
.weightText.Text, _
.heightText.Value, _
.bmiFactorNum.Text, _
.bmiFactorText.Text)
End With
Application.EnableEvents = True' turn events handing on
End Sub