How To: Use the Field Calculator to populate a field with ordered numbers
Instructions provided explain how to populate a field in a feature class or stand alone table (DBF, CSV, etc.) with ascending, or ordered values, using an initial value and a designated interval.
Note: The content in this article pertains to ArcGIS versions 8.x and 9.x. Later versions of ArcGIS may contain different functionality, as well as different names and locations for menus, commands and geoprocessing tools.
To calculate a field using consecutive or stepped values:
- In the attribute table, add a Long Integer type field. If calculating an existing field, make sure it is a Long Integer Type.
- Right-click the heading of the new field.
- Select Calculate Values (9.1 and previous) or Field Calculator (9.2 and later).
- Click Yes when asked whether or not to calculate outside an edit session.
- In the Field Calculator dialog box, check Advanced. Copy and paste the following code into the PreLogic VBA Script Code box:
Static rec As Long Dim lStart As Long Dim lInterval As Long Dim i As Integer Dim iStringLength As Integer Dim sID As String '============================ 'set the variables below iStringLength = 7 lStart = 1 lInterval = 1 '============================ sID = "" If (rec = 0) Then rec = lStart Else rec = rec + lInterval End If For i = 1 To iStringLength - Len(CStr(rec)) sID = sID & "0" Next i sID = sID & CStr(rec)
- If necessary, set the values for the variables iStringLength, lStart, and lInterval where indicated. lStart is the first value; lInterval is the step interval. For example, to count by 10, set lStart = 10 and lInterval = 10.
- In the bottom input box of the Field Calculator dialog box, enter sID.
- Click OK in the Field Calculator dialog box.