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.


To calculate a field using consecutive or stepped values:

  1. In the attribute table, add a Long Integer type field. If calculating an existing field, make sure it is a Long Integer Type.
  2. A. Right-click the heading of the new field.
    B. Select 'Calculate Values' (9.1 and previous) or 'Field Calculator' (9.2 and later).
    C. Click Yes when asked whether or not to calculate outside an edit session.
  3. 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
    rec = rec + lInterval
    End If
    For i = 1 To iStringLength - Len(CStr(rec))
    sID = sID & "0"
    Next i
    sID = sID & CStr(rec)

  4. 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.
  5. In the bottom input box of the Field Calculator dialog box, enter 'sID' without quotes.
  6. Click OK in the Field Calculator dialog box.