I'm going to start with a cell formula approach, then give the vba equivalent.
The easiest place to start would be to start with a reference table that contains your logic.
ie in columns
"A" "B"
1223 "Check Width"
1234 "Do Not Use"
1134 "Use with caution"
Lets say that the part number is given in cell "C1".
If you use a cell to place your text messages you could use the formula
Formula:
=IFERROR(INDEX(A1:B3, MATCH(C1, A1:A3, 0), 2),"No Warning")
If you wanted that as a vba code, such as a function returning a text, the same approach would work
Private Function getWarning(partNum As String) As String
Dim rng As Range
Set rng = Sheet1.Range("A1:B3")
With Application.WorksheetFunction
getWarning = .IfError(.Index(rng, .Match(partNum, rng.Columns(2), 0), 2), "No Warning")
End With
End Function
Or using a vba error handling approach
Private Function getWarning(partNum As String) As String
Dim rng As Range
On Error GoTo errHandle
Set rng = Sheet1.Range("A1:B3")
With Application.WorksheetFunction
getWarning = .Index(rng, .Match(partNum, rng.Columns(2), 0), 2)
End With
Exit Function
errHandle:
getWarning = "No Warning"
End Function
hence whenever if you want to set the warning in textbox2 when the value of textbox1 changes in a userform
Private Sub textbox1_change()
textbox2.Value = getWarning
End Sub
I hope that this helps
Bookmarks