I know this question has been asked a hundred times on here, for example:
- Basic If/Then in Python Parser of ArcGIS Field Calculator?
- Using if/elif/else statements in ArcGIS Field Calculator?
- Checking for values in one column and do something in another using ArcGIS Field Calculator?
- https://community.esri.com/thread/21689
but I just can't get my code to work. I am trying to do a simple if/then statement in field calculator. I have a column (risk_value) I want to populate based on the values in the other (risk_color). There are only four values in Risk_color: Green, yellow, red, null.
I tried writing code in VBA:
Pre-logic script Code:
Dim val
If [RISK_COLOR] = "Green" Then
val = "Low"
elseif [RISK_COLOR] = "Yellow" Then
val = "Medium"
elseif [RISK_COLOR] = "Red" Then
val = "High"
elseif [RISK_COLOR] = "<Null>" Then
val = "<Null>"
end if
RISK_VALUE=
val
Error executing function.
Then I tried python string:
def Value(color):
if (color == 'Green'):
return "Low"
elif (color == 'Yellow'):
return "Medium"
elif (color == 'Red'):
return "High"
else:
return "<Null>"
RISK_VALUE=Value(!RISK_COLOR!)
Error executing function.
What am I doing wrong?
1 Answer 1
It is how you are dealing with Null values. You are treating Null as a string when it is NOTHING! How can nothing be something?
But now think about what you are asking, in your case if colour is not green, yellow or red then it can only be a Null value. So you don't need to test if it is Null as you have already checked all other options.
So your VB script code need only be:
Dim val
If [RISK_COLOR] = "Green" Then
val = "Low"
elseif [RISK_COLOR] = "Yellow" Then
val = "Medium"
elseif [RISK_COLOR] = "Red" Then
val = "High"
else
val = "<Null>"
end if
Explore related questions
See similar questions with these tags.
'Red''
, but good catch.