簡體   English   中英

VBA用戶表單輸入驗證錯誤

[英]VBA Userform input validation error

我是VBA的新手,但我的問題可能很愚蠢,但我無法解決,因此,請盡我所能!

事情是這樣的:我得到的用戶窗體可以完美地填充電子表格,但是如果不輸入信息,它就會發瘋。 正如您可能在下面看到的那樣,我發現了一些代碼來檢查是否輸入了數據,因此,如果未彈出該窗口,則必須輸入一些內容,但是當您執行表單時,它會填充兩行數據而不是一行。 例如,如果選擇行“ x”並要放置值“ a”,“ b”,“ c”,“ d”,但忘記放置值“ c”,那么它將顯示錯誤,並且當我鍵入缺少值“ c',然后按OK,將創建具有值'a','b',','d'的行'x'和具有值'a','b','c','d的行'x + 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
'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

結束子

先感謝您

在驗證檢查之后,嘗試將代碼“將用戶表單條目寫入數據庫”移動到。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM