Use the VBScript option for the Expression Type parameter of the Calculate Field or Calculate Fields (multiple) tool, if you have VBA or VBScript experience and are comfortable with the scripting syntax. This option also supports legacy models or scripts from ArcGIS Desktop that use the Calculate Field tool with VBScript calculations.
This topic focuses on VBScript-based Calculate Field examples. To learn more about Python examples, see Calculate Field Python examples. To learn more about Arcade expressions, see the ArcGIS Arcade guide. To learn more about SQL expressions, see Calculate field values.
VBScript does not allow you to explicitly declare any data type; all variables are implicitly Variant. Statements such as Dim x as String should be removed or simplified to Dim x.
A variety of calculations can be calculated with only a short expression.
VBScript string functions
Strings are supported by a series of VBScript string functions, including Left, InStr, and Chr. The following are VBScript examples for commonly used string functions in the Field Calculator:
Use the Left function to return a specified number of characters from the left side of a string.
Expression: Left([MyField], 1)
Use the Right function to return a specified number of characters from the right side of a string.
Expression: Right([MyField], 1)
Use the Mid function to return a specified number of characters from a string using a start position and an optional number of characters.
Expression: Mid([MyField], 14, 4)
Use the InStr function to return the position of the first occurrence of one string within another.
Expression: InStr([MyField], " ")
Use the Replace function to return a string in which a specified substring has been replaced with another substring.
Expression: Replace([MyField], "#", "!")
Use the Chr function to return the character associated with the specified character code.
The following example replaces a carriage return character with an exclamation point.
Expression: Replace([MyField], Chr(13), "!")
Use the & operator to concatenate two field values.
Expression: [MyField1] & " " & [MyField2]
Simple math examples
VBScript provides tools for processing numbers.
x + y
x plus y
1.5 + 2.5
x - y
x minus y
3.3 - 2.2
x * y
x times y
2.0 * 2.2
x / y
x divided by y
3 / 2
x \ y
x divided by y (integer division)
3 \ 2
x MOD y
x modulo y
8 MOD 3
x ^ y
x raised to the power of y
2 ^ 3
Multiply the values of a field by 2.
[Rank] * 2
Calculate fields using logic with VBScript
Logical patterns can be included in a code block using If, ElseIf, and Else statements.
Classify based on field values.
Expression: density Code Block: Dim density If [POP90_SQMI] < 100 Then density = "low" ElseIf [POP90_SQMI] < 300 Then density = "medium" Else density = "high" End If