2013-07-29 37 views
0

我是VBA的新手,我的问题可能很愚蠢,但我无法修复它,所以请帮助我,如果可以的话!VBA Userform输入验证错误

这是事情:我得到了一个用户表单,它完美地填充了电子表格,但是如果没有输入信息,它会发生疯狂的事情。正如你在下面看到的,我发现了一些代码来检查数据是否被输入,所以如果它没有弹出窗口,你必须输入一些东西,但是当你做表格填充2行数据而不是一行。例如,如果选择'x'行并希望将值'a','b','c','d',但忘记将值'c',那么它会显示错误,并且当我键入缺失值' c'并按下OK,它将创建值为'a','b','','d'和行'x + 1'且值为'a','b','c','d'的行'x' ”。 这里是我的代码:

Private Sub cmdok_Click() 
'next empty cell in column A 
Set c = Range("a65536").End(xlUp).Offset(1, 0) 
    Application.ScreenUpdating = False 'speed up, hide task 
'write userform entries to database 
c.Value = Me.txtFname.Value 
c.Offset(0, 3).Value = Me.txtngoals.Value 
c.Offset(0, 28).Value = Me.cmbDiag.Value 

If Me.optAcute.Value = "True" And Me.optchronic.Value = "False" Then 
    c.Offset(0, 29).Value = 1 
    c.Offset(0, 30).Value = "" 
Else 
    c.Offset(0, 29).Value = "" 
    c.Offset(0, 30).Value = 1 
End If 

'input validation 
If txtFname.Value = "" Then 
    MsgBox ("Sorry, you need to provide a Name") 
    txtFname.SetFocus 
Exit Sub 
End If 

If txtngoals.Value = "" Then 
    MsgBox ("Sorry, you need to provide goals") 
    txtngoals.SetFocus 
Exit Sub 
End If 

If cmbDiag.Value = "" Then 
    MsgBox ("Sorry, you need to provide Diagnosis") 
    cmbDiag.SetFocus 
Exit Sub 
End If 

If optAcute.Value = optchronic.Value Then 
    MsgBox ("Sorry, you need to select Time since injury") 
    Exit Sub 
End If 

'clear the form 
With Me 
    .txtFname.Value = vbNullString 
    .cmbDiag.Value = vbNullString 
    .optAcute.Value = vbNullString 
    .optchronic.Value = vbNullString 
    .txtngoals.Value = vbNullString 
End With 
Application.ScreenUpdating = True 

末次

预先感谢您

回答

1

尝试移动代码“写入用户窗体输入到数据库”,以验证检查后。

Private Sub cmdok_Click() 
'next empty cell in column A 
Set c = Range("a65536").End(xlUp).Offset(1, 0) 
    Application.ScreenUpdating = False 'speed up, hide task 

'input validation 
If txtFname.Value = "" Then 
    MsgBox ("Sorry, you need to provide a Name") 
    txtFname.SetFocus 
Exit Sub 
End If 

If txtngoals.Value = "" Then 
    MsgBox ("Sorry, you need to provide goals") 
    txtngoals.SetFocus 
Exit Sub 
End If 

If cmbDiag.Value = "" Then 
    MsgBox ("Sorry, you need to provide Diagnosis") 
    cmbDiag.SetFocus 
Exit Sub 
End If 

If optAcute.Value = optchronic.Value Then 
    MsgBox ("Sorry, you need to select Time since injury") 
    Exit Sub 
End If 

'write userform entries to database 
c.Value = Me.txtFname.Value 
c.Offset(0, 3).Value = Me.txtngoals.Value 
c.Offset(0, 28).Value = Me.cmbDiag.Value 

    If Me.optAcute.Value = "True" And Me.optchronic.Value = "False" Then 
    c.Offset(0, 29).Value = 1 
    c.Offset(0, 30).Value = "" 
    Else 
    c.Offset(0, 29).Value = "" 
    c.Offset(0, 30).Value = 1 
    End If  

'clear the form 
With Me 
    .txtFname.Value = vbNullString 
    .cmbDiag.Value = vbNullString 
    .optAcute.Value = vbNullString 
    .optchronic.Value = vbNullString 
    .txtngoals.Value = vbNullString 
End With 
Application.ScreenUpdating = True 
+0

谢谢RowanC! 问题解决了。 – user2628546

+0

@ user2628546 ..你必须将其标记为答案! – matzone