基于用户input的数据进行validation

我正在尝试创build一个实验室结果数据input表单,它根据所testing产品的规格validation答案。 用户input以下信息:产品代码和SG结果等

我的源数据是一个有4列的表,产品代码,说明,SG低,SG高

资源

源数据

当用户input产品代码和SG时,我希望根据产品允许的特定范围(来自源数据)进行validation,并且有一个对话框要求用户重新考虑input的结果(如果是超出了范围)。

很容易在结果表中用条件格式标记,但我不希望我的用户有权访问它。

结果

超出规定标志的结果

我需要参考单独的范围VLOOKUP来返回规格。

表格 用户输入表单

提前致谢!

(更新)

Private Sub CommandButton1_Click() Dim i As Integer i = 2 While ThisWorkbook.Worksheets("Sheet2").Range("A" & i).Value <> "" i = i + 1 Wend Dim losg, loph, hisg, hiph As Double losg = Application.WorksheetFunction.VLookup(ProdCode.Text, Sheet1.Range("A1:F24"), 3, False) hisg = Application.WorksheetFunction.VLookup(ProdCode.Text, Sheet1.Range("A1:F24"), 4, False) loph = Application.WorksheetFunction.VLookup(ProdCode.Text, Sheet1.Range("A1:F24"), 5, False) hiph = Application.WorksheetFunction.VLookup(ProdCode.Text, Sheet1.Range("A1:F24"), 6, False) If SGresult.Text < losg Then MsgBox "SG result " & SGresult.Text & " too low" ElseIf SGresult.Text > hisg Then MsgBox "SG result " & SGresult.Text & " too high" Else: MsgBox "SG result " & SGresult.Text & " just right" End If If pHresult.Text < loph Then MsgBox "ph result " & pHresult.Text & " too low" ElseIf pHresult.Text > hiph Then MsgBox "ph result " & pHresult.Text & " too high" Else: MsgBox "ph result " & phresult.Text & " just right" End If ThisWorkbook.Worksheets("Sheet2").Range("A" & i).Value = ProdCode.Value 'Enter Code in Column A ThisWorkbook.Worksheets("Sheet2").Range("C" & i).Value = BNenter.Value 'Enter BN in Column C ThisWorkbook.Worksheets("Sheet2").Range("D" & i).Value = DOMenter.Value 'Enter DOM in Column D ThisWorkbook.Worksheets("Sheet2").Range("E" & i).Value = SGresult.Value 'Enter SG result in Column E ThisWorkbook.Worksheets("Sheet2").Range("F" & i).Value = pHresult.Value 'Enter pH result in Column F ThisWorkbook.Worksheets("Sheet2").Range("K" & i).Value = BatcherID.Value 'Enter Batcher ID in Column K End Sub 

将产品保存在“K”栏中,对于“L”栏中各个产品的有效结果。 下面的代码会给你想要的输出

 Dim result, prod As String Dim rng As Range result = Val(resultText.Value) prod = prodText.Value ActiveSheet.Activate On Error GoTo step: Set rng = Range("K:K").Find(What:=prod, LookIn:=xlValues, LookAt:=xlWhole) If rng.Offset(0, 1).Value <> result Then MsgBox "The result entered is out of valid range!" End If Exit Sub step: MsgBox "Invalid Product" Exit Sub 

OP在澄清“表格”之后编辑了一个“UserFom”

您可能想要在编辑/退出任何控件时检查用户input,而不是等待CommandButton1_Click事件并一起检查它们

这样的“模块化”方法应该使代码更容易控制和维护

例如, TextBox Exit事件可以用来检查用户input,因为他/她正在离开它,并且在input错误的情况下让他/她回来

此外

  • 由于必须在“源”工作表列“A”中列出的“产品代码”

    您可能需要使用ComboBox控件并让用户从列表中select一个

  • 由于“产品名称”必须是与所选的“产品代码”对应的产品名称,

    您可能需要使用Label控件,并让用户只需查看他刚刚select的产品代码的名称

按照上面的内容,假设“ProductNameLbl”作为标签名称,您的用户表单代码可能如下所示:

 Option Explicit Private Sub UserForm_Initialize() Me.ProdCodeCB.List = GetSourceData(1) '<--| fill Product Name combobox list with "Source" worksheet column 1 data End Sub Private Sub ProdCodeCB_Change() '<--| fires when the user change the combobox selection Me.ProdNameLbl.Caption = Worksheets("Source").Cells(Me.ProdCodeCB.ListIndex + 2, 2) '<--| update Product Name label with the name corresponding to the chosen Product Code End Sub Private Sub SGresultTB_Exit(ByVal Cancel As MSForms.ReturnBoolean) '<--| fires upon exiting the SGresult textbox Dim msgErr As String With Me '<--| reference the Userform If .ProdCodeCB.ListIndex <> -1 Then '<--| if a valid selection has been made in 'ProductCode' combobox If Not IsValueInRange(.SGresultTB, GetProdCodeRange(.ProdCodeCB.ListIndex + 1), msgErr) Then '<-- if value out of range then... With .SGresultTB MsgBox "SG value " & .Value & msgErr _ & vbCrLf & vbCrLf & "Please reconsider the value you input in 'SG' texbox" Cancel = True .SetFocus '<--| get the user back to the textbox ' following two lines select the textbox text so that the user can delete it .SelStart = 0 .SelLength = Len(.Text) End With End If End If End With End Sub '------------------------------------------------- ' helper functions '--------------------------- Function GetSourceData(colIndex As Long) ' this function returns an array with "Source" worksheets data in passed column from its row 2 to last not empty one With Worksheets("Source") '<--| reference "Source" worksheet GetSourceData = Application.Transpose(.Range(.Cells(2, colIndex), .Cells(.Rows.Count, colIndex).End(xlUp)).Value) End With End Function Function IsValueInRange(tb As MSForms.TextBox, rangeArr As Variant, msgErr As String) As Boolean ' this function returns a boolean (true/false) with the result of the checking whether the passed texbox (tb) text exceeds the passed range (rangeArr) ' msgErr is also set to some text if the range is exceeded With tb Select Case CDbl(.Value) '<-- prepare to act accordingly to its value Case Is < rangeArr(1) '<--| if it's smaller than "SG Low" value msgErr = " is lower than 'SG Low' = " & rangeArr(1) '<-- build the final part of the error message correspondingly Case Is > rangeArr(2) '<--| while if it's greater than "SG High" value msgErr = " is greater than 'SG High' = " & rangeArr(2) '<-- build the final part of the error message correspondingly End Select End With IsValueInRange = msgErr = "" End Function Function GetProdCodeRange(iProd As Long) ' this function returns an array of the SG minimum and maximum values in "Source" worksheet corresponding to the chosen product With Worksheets("Source") '<--| reference "Source" worksheet With .Range("A2", .Cells(.Rows.Count, "A").End(xlUp)) '<--| reference its column "A" cels from row 2 down to last not empty one GetProdCodeRange = Application.Transpose(Application.Transpose(.Cells(iProd, 1).Offset(, 2).Resize(, 2).Value)) '<--| return an array with "SG low" and "SG high" values corresponding to the product index passed End With End With End Function '------------------------------------------------- 

正如你所看到的,我为你select的名字命名控件,除了添加一个后缀来说明它们是什么types的控件:

  • ProdCodeCB:“CB” – >这是一个ComboBox控件名称

  • SGresultTB:“TB” – >这是一个TextBox控件名称

  • ProdNameLbl:“Lbl” – >这是一个Label控件名称