How To: Calculate X, Y, and Z values in an attribute table using Field Calculator
Note: These instructions are for ArcGIS 8.x and 9.x only. ArcGIS version 10.0 is the last version of ArcGIS to support Microsoft Visual Basic for Applications VBA).
Note: For ArcGIS Desktop 10.x, the same results can be obtained using the Add XY Coordinates and Calculate Geometry Attributes tools. Refer to the Related Links section for more information.
This article provides instructions to calculate X, Y, and Z values in an attribute table using the Field Calculator and Visual Basic for Applications (VBA).
Note: These code examples return a type 'double' value, so use them to calculate either an existing field of type 'double' or a new field of type 'double' that has been added to the table.
This procedure can be used with new or existing fields in an attribute table. If it is necessary to add new fields to the attribute table, refer to the ArcGIS Desktop 9.3 Help: Adding and deleting fields in a table for more information.
- Add the point layer to ArcMap.
- Start Editing.
- Select Editor Toolbar from the Tools menu if the Editor Toolbar is not open.
- Click Editor, and select Start Editing from the pop-up menu.
- Select the data layer for the list box, and click OK if prompted by the Start Editing dialog box.
- Set the Target drop-down list to the layer to edit.
- Right-click the layer, and select Open Attribute Table.
- Right-click the field to modify, and select Calculate Values.
- Check Advanced. Two empty text entry boxes are present.
- Paste the following code into the Pre-Logic VBA Script Code text box:
Dim Output As Double Dim pPoint As IPoint Set pPoint = [Shape] Output = pPoint.X
- In the Expression line, the small, one line area below the Pre-Logic box, type Output and click OK to calculate the field.
Note: The units of the calculated values are the units the features are stored in, not the map units or display units of the data frame currently worked on.
Note: If the data is stored in feet, the calculated values appear in feet. To display calculated data in different units other than the data's units, perform one of the following: -Add a conversion into the calculation expression. -Project the data into a coordinate system that uses the units the values should be in, and perform the calculation.
- Use the procedure described above for the Y and Z fields by changing the Output value.
To calculate Y:
Output = pPoint.Y
To calculate Z:
Output = pPoint.Z
Note: A 3D Analyst license is not required to extract Z values from the shape field of an existing 3D layer.
- FAQ: Is it possible to use VBA Script Code for the Calculate Field tool in ArcToolbox?
- ArcMap: Calculate Field examples
- ArcMap: Add XY Coordinates
- ArcMap: Calculate Geometry Attributes
- ArcGIS Pro: Add XY Coordinates (Data Management)
- ArcGIS Pro: Calculate Geometry Attributes