2013-07-29 38 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