用户窗体在按下按钮VBA后消失
我有一个程序,我必须计算体重指数。 它工作得很好,除了一件事,当我按下“New Entry”按钮向电子表格添加条目时,userform消失了,但userform的窗口框架没有消失 请参阅所附图片:用户窗体在按下按钮VBA后消失,vba,excel,userform,Vba,Excel,Userform,我有一个程序,我必须计算体重指数。 它工作得很好,除了一件事,当我按下“New Entry”按钮向电子表格添加条目时,userform消失了,但userform的窗口框架没有消失 请参阅所附图片: 以下是“新建条目”按钮的代码: 也许所有的激活都会造成伤害。。。 或者在每次单元格写入时都会触发一些事件处理 请尝试使用此代码 Option Explicit Private Sub newEntryButton_Click() Dim targetSht As Worksheet
以下是“新建条目”按钮的代码:
也许所有的
激活
都会造成伤害。。。
或者在每次单元格写入时都会触发一些事件处理
请尝试使用此代码
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
也许所有的
激活
都会造成伤害。。。
或者在每次单元格写入时都会触发一些事件处理
请尝试使用此代码
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
尝试用F8单步执行代码以确定问题所在。来自令人惊叹的Pearson芯片的好信息。我也会将错误捕获添加到您的过程中。尝试用F8单步遍历代码以确定问题所在。来自神奇芯片Pearson的好信息。我也会在您的过程中添加错误捕获。我相信
Exit Sub
应该在Case Else
语句下?我相信Exit Sub
应该在Case Else
语句下?