Kenja824
Automotive
- Nov 5, 2014
- 950
I have code that was written for me once that I just learned has a flaw for getting the correct outcome. This function below is to read a long material spec in a cell and let me know if it is a G (Galvanized), B (uncoated or Bare), XG, XB, VG or VB which are all Galvanized or Uncoated high strength.
The problem is this code says if the spec has "uncoated" in the spec it is "B" (which is correct) If it has "-U*" It is "G". Which is not correct because the Uncoated materials also often have the -U in it.
What I need to do is change it so that if it has "Uncoated" in the spec, it is a "B" and if it does not have "Uncoated" in the spec, it is a "G".
I know excel IF statements in formulas are written so the last segment is the result if it is NOT what the others are. Can we do something like that in VB code?
How do I fix this code?
Public Function Material_code(x_order_string As String) As String
'This function searches through an order string for certain values, then returns a code based on those values
'dimension my variables as strings/text
Dim xorv As String
Dim gorb As String
Dim final_output As String
'give initial values of nothing - not v or b, not g or b to each variable
xorv = ""
gorb = ""
final_output = ""
'check the order string to see if it should be a x or v value, then g or b and set variables as appropriate
If x_order_string Like "*270LA*" Or x_order_string Like "*CR270*" Or x_order_string Like "*CR300*" Or x_order_string Like "*300LA*" Or x_order_string Like "*340_LA*" Or x_order_string Like "*340LA*" Or x_order_string Like "*380LA*" Or x_order_string Like "*420LA*" Or x_order_string Like "*500LA*" Or x_order_string Like "*550LA*" Then
xorv = "X"
ElseIf x_order_string Like "*T/*" Then
xorv = "V"
End If
If x_order_string Like "*UNCOATED*" Then
gorb = "B"
End If
If x_order_string Like "*-U*" Then
gorb = "G"
ElseIf x_order_string Like "*-E*" Then
gorb = "G"
End If
'combine two variables for final output code, then check for single digit values
final_output = xorv & gorb
If final_output = "X" Then
final_output = "XG"
End If
If final_output = "V" Then
final_output = "VG"
End If
Material_code = final_output
End Function
The problem is this code says if the spec has "uncoated" in the spec it is "B" (which is correct) If it has "-U*" It is "G". Which is not correct because the Uncoated materials also often have the -U in it.
What I need to do is change it so that if it has "Uncoated" in the spec, it is a "B" and if it does not have "Uncoated" in the spec, it is a "G".
I know excel IF statements in formulas are written so the last segment is the result if it is NOT what the others are. Can we do something like that in VB code?
How do I fix this code?
Public Function Material_code(x_order_string As String) As String
'This function searches through an order string for certain values, then returns a code based on those values
'dimension my variables as strings/text
Dim xorv As String
Dim gorb As String
Dim final_output As String
'give initial values of nothing - not v or b, not g or b to each variable
xorv = ""
gorb = ""
final_output = ""
'check the order string to see if it should be a x or v value, then g or b and set variables as appropriate
If x_order_string Like "*270LA*" Or x_order_string Like "*CR270*" Or x_order_string Like "*CR300*" Or x_order_string Like "*300LA*" Or x_order_string Like "*340_LA*" Or x_order_string Like "*340LA*" Or x_order_string Like "*380LA*" Or x_order_string Like "*420LA*" Or x_order_string Like "*500LA*" Or x_order_string Like "*550LA*" Then
xorv = "X"
ElseIf x_order_string Like "*T/*" Then
xorv = "V"
End If
If x_order_string Like "*UNCOATED*" Then
gorb = "B"
End If
If x_order_string Like "*-U*" Then
gorb = "G"
ElseIf x_order_string Like "*-E*" Then
gorb = "G"
End If
'combine two variables for final output code, then check for single digit values
final_output = xorv & gorb
If final_output = "X" Then
final_output = "XG"
End If
If final_output = "V" Then
final_output = "VG"
End If
Material_code = final_output
End Function