ArcGIS – Fixing VBScript if/then Syntax Error 000539 in Field Calculator

arcgis-10.1arcgis-desktoperror-000539field-calculatorvbscript

What is wrong with this VBscript in the field calculator?

ColumnC=

if [ColumnB] > 0 then
    [ColumnC] = [ColumnA] / [ColumnB]
endif

Each time I receive this syntax error message:

Error 000539: SyntaxError: invalid syntax (expression, line 1) Failed
to execute

All 3 columns are the double field type, although they vary in precision and scale. I have tried converting the columns to float type and doing the same expression. I received the same error messages.

For the first line (starting with 'if'), the script is left-justified. For the second line (starting with '[ColumnC]) I included four spaces before the script. In the codeblock on the field calculator the equation does extend into the third line (and is left justified) simply because the actual name I use for !ColumnB! is longer.

Best Answer

You need to pass the returned value in a variable, not reference the column itself.

Expression:

result

Codeblock:

if [ColumnB] > 0 then
    result = [ColumnA] / [ColumnB]
else
    result = 0
end if

Here's how the two parts fit together in the field calculator - my data doesn't have the same field names, but you get the idea.

enter image description here

Related Question