验证文本框输入

时间:2016-01-16 16:43:45

标签: vba excel-vba excel

我在Excel中创建了一个userform,并有一个数据验证范围。

我想在用户点击"保存"之前检查用户的输入。按钮。

例如:值输入为&#34; x&#34;,如果1 <= x <= 10,数据文本框将标记为红色突出显示并弹出消息&#34;失控&#34;,表单将请求用户继续输入&#34;重新测试&#34;数据范围内的文本框(在Excel文件中)。

这也是我的代码和附件文件 http://www.mediafire.com/view/fl1nk84nu62wg34/Control_Chart.xlsm

Private Sub cbCa_DropButtonClick()
    cbCa.AddItem ("Ca 1")
    cbCa.AddItem ("Ca 2")
    cbCa.AddItem ("Ca 3")
    cbCa.AddItem ("")
End Sub

Private Sub cbType_DropButtonClick()
    cbType.AddItem ("Set Up")
    cbType.AddItem ("Production")
End Sub

Private Sub CommandButton1_Click()
    'Copy input values to sheet.
    Dim lRow As Long
    Dim ws As Worksheet

    Set ws = Worksheets("Input Data")
    lRow = ws.Cells(Rows.Count, 8).End(xlUp).Offset(1, 0).Row
    With ws
        .Cells(lRow, 2).Value = Me.txNgay.Value
        .Cells(lRow, 3).Value = Me.txGio.Value
        .Cells(lRow, 4).Value = Me.cbCa.Value
        .Cells(lRow, 5).Value = Me.txNV.Value
        .Cells(lRow, 6).Value = Me.txSolo.Value
        .Cells(lRow, 7).Value = Me.txMa.Value
        .Cells(lRow, 8).Value = Me.txData.Value
        .Cells(lRow, 9).Value = Me.txReTest.Value
        .Cells(lRow, 10).Value = Me.txLydo.Value
        .Cells(lRow, 11).Value = Me.cbType.Value
    End With

       'Hien thong bao nhac nho
If txNgay.Text = "" Then
        MsgBox "Quen cho ngay kia thim", vbOKOnly + vbInformation, 
                 "THÔNG BÁO"

    txNgay.BackColor = &HFF& ' change the color of the textbox

    ElseIf txGio.Text = "" Then
        MsgBox "Quen nhap gio kia thim", vbOKOnly + vbInformation, 
                  "THÔNG BÁO"

        txGio.BackColor = &HFF& ' change the color of the textbox

    ElseIf txNV.Text = "" Then
        MsgBox "Ten cua thim la gi vay", vbOKOnly + vbInformation, 
                 "THÔNG BÁO"

        txNV.BackColor = &HFF& ' change the color of the textbox

    ElseIf txMa.Text = "" Then
        MsgBox "Quen nhap Ma san pham kia thim", vbOKOnly + vbInformation, 
                  "THÔNG BÁO"

        txMa.BackColor = &HFF& ' change the color of the textbox

    ElseIf txSolo.Text = "" Then
        MsgBox "Quen nhap So lo kia thim", vbOKOnly + vbInformation, "THÔNG 
                  BÁO"

        txSolo.BackColor = &HFF& ' change the color of the textbox

    ElseIf txData.Text = "" Then
        MsgBox "Quen nhap data kia thim", vbOKOnly + vbInformation, "THÔNG 
                  BÁO"

        txData.BackColor = &HFF& ' change the color of the textbox

    End If

    ThisWorkbook.Save

    '    End If

End Sub

Private Sub CommandButton2_Click()
 'Clear input controls.
    Me.txNgay.Value = ""
    Me.txGio.Value = ""
    Me.cbCa.Value = ""
    Me.txNV.Value = ""
    Me.txSolo.Value = ""
    Me.txMa.Value = ""
    Me.txData.Value = ""
    Me.txLydo.Value = ""

End Sub

Private Sub CommandButton3_Click()
  'Close UserForm.
    Unload Me
End Sub

Private Sub CommandButton4_Click()
ThisWorkbook.Sheets("Control_Chart").Visible = True
ThisWorkbook.Sheets("Control_Chart").Select
UserForm1.Hide
End Sub

Private Sub CommandButton5_Click()
Calendar1.Visible = True
End Sub

Private Sub Label15_Click()
Label15.Caption = Sheet2.Range("F2").Value
End Sub

Private Sub Label16_Click()
Label16.Caption = Sheet2.Range("F4").Value
End Sub

Private Sub Label17_Click()
Label17.Caption = Sheet2.Range("F3").Value
End Sub

Private Sub Label18_Click()
Label18.Caption = Sheet2.Range("F6").Value
End Sub

Private Sub Label20_Click()
Label20.Caption = Sheet2.Range("F5").Value
End Sub

Private Sub Label23_Click()

End Sub

Private Sub Label8_Click()
Range("F2").Select
End Sub

Public iDate As Long
Private Sub Calendar1_Click()
  iDate = Calendar1.Value
  txNgay.Value = Format(iDate, "dd/mm/yyyy")
  Calendar1.Visible = False
End Sub

Private Sub txData_Enter()
With Me.txData
        If .Value >= 0.315 Or .Value <= 0.33 Then
            .Value = ""
            MsgBox prompt:="Must be a # between 1 and 30000!", 
                Buttons:=vbCritical, Title:="Invalid Entry"
            Cancel = True
       End If
    End With
End Sub

Private Sub txData_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
 If InStr("1234567890." + Chr$(vbKeyBack), Chr$(KeyAscii)) = 0 Then
  KeyAscii = 0
 End If
End Sub

Private Sub txMa_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
 If InStr("1234567890." + Chr$(vbKeyBack), Chr$(KeyAscii)) = 0 Then
  KeyAscii = 0
 End If
End Sub


Private Sub UserForm_Initialize()
  Calendar1.Visible = False
    Label15.Visible = True
    Label16.Visible = True
    Label17.Visible = True
    Label18.Visible = True
    Label20.Visible = True
End Sub

Download the attachment file

这是我更新的代码

'Khai bao bien Data khi nguoi dung nhap lieu
Private Sub txData_AfterUpdate()
Dim x As Double 'Bien do so nhi phan
Dim F2 As Double
Dim F3 As Double
Dim F4 As Double
Dim F5 As Double
Dim F6 As Double

x = Me.txData.Value ' Set gia tri cho bien
F2 = Sheet2.Range("F2").Value
F3 = Sheet2.Range("F3").Value
F4 = Sheet2.Range("F4").Value
F5 = Sheet2.Range("F5").Value
F6 = Sheet2.Range("F6").Value

'Kiem tra hop Data Input co trong hay khong
    If Me.txData = Empty Then
        MsgBox "Please input your data", vbCritical
        txData.BackColor = &HFF& ' change the color of the textbox

        ' Kiem tra hop Data Input co phai la so hay khong
        ElseIf Not IsNumeric(Me.txData) Then
        MsgBox "Numberic only", vbCritical
        txData.BackColor = &HFF& ' change the color of the textbox
        Else
        'Xet dieu kien nguoi dung nhap vao
        Select Case x
            Case x = 0
                MsgBox "Data sao bang 0 duoc", vbCritical
                txData.BackColor = &HFF& ' change the color of the textbox
                txData.SetFocus
             Case Is < F6
                MsgBox "Lower Out of Control. Ban hay nhap gia tri vao o Re Test", vbCritical
                txData.BackColor = &HFF& ' change the color of the textbox
                txReTest.SetFocus
             Case Is > F4
                MsgBox "Upper Out of Control. Ban hay nhap gia tri vao o Re Test", vbCritical
                txData.BackColor = &HFF& ' change the color of the textbox
                txReTest.SetFocus
             Case F5
                MsgBox "Database is correct", vbInformation
                End Select
        ThisWorkbook.Save
    End If

End Sub

1 个答案:

答案 0 :(得分:0)

由于您将x设置为文本框的值,因此您不需要在案例选择中使用if语句。

Select Case x
    Case 0.3
        MsgBox "Correct"
    Case Is < 0.3
        MsgBox "Lower limit"
    Case Is > 0.3
        MsgBox "Upper Limit"
End Select

请注意,我使用的限制不一定与您的限制相对应。

要验证文本框,您可以编写一个afterupdate sub,当您通过按Tab键取消选择文本框时会触发该子文件。它可能看起来像下面的子类,要知道要么为要检查的每个输入写一个子,如果你有很多类似的输入,你可以编写一个从afterupdate子调用的私有子。检查文本框的值是否为数字,并且位于某些参数内。

Private Sub txNgay_AfterUpdate()
    If Me.txNgay = Empty Then
        'Code which sets the the look of textbox back to normal.
    ElseIf Not IsNumeric(Me.txNgay) Then
        MsgBox "The textbox doesn't contain a numerical value.", vbCritical
        'Code which sets the look of the textbox to a faulty state
    ElseIf Me.txNgay < 1 Or Me.txNgay > 10 Then
        MsgBox "The value is out of range"
        'Code which sets the look of the textbox to a faulty state
    Else
        'Code which sets the look of the textbox to a normal state
    End If
End Sub

你甚至可以在模块中包含一个私有子,每次更新一个字段时会调用它来检查所有输入,只有在满足所有条件时才激活OK按钮。

您可以使用大小写选择来执行基于值的代码

Select Case x
    Case 0
        'Code to perform the actions you need if x is 0
    Case 1 To 10
        'Code to perform the actions you need if x is between 1 and 10
    Case Else
        'Code to perform the actions you need if x is < 0 and x > 10
End Select
相关问题