2016-10-01 5 views
0

Ich versuche ein Formular für die Dateneingabe von Laborergebnissen zu erstellen, das eine Antwort basierend auf der Spezifikation des getesteten Produkts validiert. Der Benutzer gibt die folgenden Informationen: Produktcode und SG-Ergebnis etcValidierung basierend auf vom Benutzer eingegebenen Daten

Meine Quelldaten ist eine Tabelle mit 4 Spalten, Produktcode, Beschreibung, SG niedrig, SG Hoch

SOURCE

Source data

Wenn der Benutzer den Produktcode und das SG in dem Formular eingibt, das ich validieren möchte, basierend auf dem für das Produkt zulässigen Bereich (aus den Quelldaten), und ein Dialogfeld mit der Aufforderung, das eingegebene Ergebnis zu überprüfen (wenn es außerhalb des Bereichs wäre).

Einfach genug, um mit bedingter Formatierung im Ergebnisblatt zu kennzeichnen, aber ich möchte nicht, dass meine Benutzer darauf zugreifen können.

ERGEBNISSE

Results with out of spec flag

Ich muss verweisen Bereich SVERWEIS zu trennen, die Spezifikationen zurückzukehren.

DIE FORM the user entry form

Vielen Dank im Voraus!

(Update)

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 
+0

Sie das Formular Ergebnis sogar annehmen möchten Sie obwohl es außerhalb des Bereichs? –

+0

Hi @AdityaPansare, ich möchte ein out-of-range-Ergebnis akzeptieren, aber das Popup soll dem Benutzer vorschlagen, das Ergebnis zu überprüfen (ich könnte dem Formular ein Kommentarfeld hinzufügen, wenn es erklärt werden soll). – Steamroller60

+0

Ich muss SVERWEIS verwenden, da der Bereich der zulässigen Werte je nach Produkt unterschiedlich ist – Steamroller60

Antwort

1

Speicher Produkte in der Spalte "K" und gültiges Ergebnis für jeweiliges Produkt in der Spalte "L".Im Folgenden Code wird Ihnen gewünschte Ausgabe

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 
0

bearbeitet geben nach OP die „Form“ verdeutlicht ein „UserFom“ war

Möglicherweise möchten Sie Benutzereingaben überprüfen, während er/sie gerade bearbeite/Verlassen stattdessen jede Kontrolle des Wartens auf die CommandButton1_Click Ereignis und überprüfen sie sie alle zusammen

eine solche „modular“ Ansatz sollte Code halten mehr leicht zu steuern und

zum Beispiel halten die TextBoxExit Ereignis verwendet werden, um die Benutzereingabe zu überprüfen, wie er/sie es verlassen hat und hat ihn/sie

es bei falscher Eingabe

Außerdem

  • seit „Product Code“ zurückkommen sein muss zwischen denen in „Source“ Arbeitsblatt Spalte „A“

    Sie eine ComboBox Steuerelement verwenden möchten aufgelistet gewählt und haben die Benutzer eine aus einer Liste auswählen

  • seit „Produktname“ muss t sein er einer der gewählten „Produktcode“ entsprechenden

    Sie möchten eine Label Steuerelement verwenden und haben die Benutzer einfach sieht zu welchem ​​Namen dem Produktcode entspricht wählte er nur

Nach was oben und etwas könnte „ProductNameLbl“ als Markennamen, Ihre Userform Code unter der Annahme sein, wie folgt:

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 
'------------------------------------------------- 

wie Sie sehen können, ich Kontrollen nach den Namen benannt Sie für sie wählen mit Ausnahme ein Suffix zu sagen, welche Art von Kontrolle, die sie sind:

  • ProdCodeCB: "CB" -> es ist ein ComboBox Steuer Name

  • SGresultTB: "TB" -> es ist ein TextBox Steuer Name

  • ProdNameLbl: "Lbl" -> es ist ein Label Steuer Name

Verwandte Themen