简体   繁体   中英

VBA Userform input validation error

I am new to VBA and my problem might be stupid, but I cant fix it, so please help me if you can!

Here is the thing: I got userform that fills spreadsheet perfectly, but if information is not entered it does crazy things. As you may see below i found some piece of code to check if the data is entered, so if its not window pops up and you have to enter something, but when you do form fills 2 rows of data instead of one. For example, if pick row 'x' and want to put values 'a','b','c','d', but forgot to put value 'c' then it shows an error and when i type missing value 'c' and press OK it creates row 'x' with values 'a','b',' ','d' and row 'x+1' with values 'a','b','c','d'. Here is my code:

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

End Sub

thank you in advance

Try moving the code "write userform entries to database" to after the validation checks.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM