0

私は VBA に慣れていないので、私の問題はばかげているかもしれませんが、修正できないので、できれば助けてください!

ここに問題があります。スプレッドシートを完全に埋めるユーザーフォームを取得しましたが、情報が入力されていないと、おかしなことになります。以下に示すように、データが入力されているかどうかを確認するコードを見つけたので、そうでない場合はウィンドウがポップアップし、何かを入力する必要がありますが、フォームを入力すると、1 行ではなく 2 行のデータが入力されます。たとえば、行「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

サブ終了

前もって感謝します

4

1 に答える 1

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
于 2013-07-29T02:39:54.713 に答える