HOW TO

Label expression by way of VBScript

Last Published: April 25, 2020

Summary

Instructions provided describe how to label expressions by way of VBScript. ArcMap provides a way to programmatically define the text that displays when labeling features.

This article contains sample code that show how to perform these common functions: case formatting, extracting a number, and stacking of labels.

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.

Procedure

  1. Right-click the layer and select Properties, switch to the Label tab, check the Label Features in this layer, and click Expression. In the Expression Properties dialog box check the Advanced option.
  2. Copy one of the VBScript functions below into the Expression field.
  3. Substitute the field [NAME] with a field that exists in your layer.
  • Convert field name to mixed case:
Code:
Function FindLabel ( [NAME] )
' Converts [NAME] to Mixed Case
' -----------------------------
Dim t, newS, i

  t = [NAME]
  newS = UCase(Mid(t, 1, 1)) 'Upcase 1st char
  i = 2
  Do While i <= Len(t)
    ' check if previous char is a space
    If InStr(1, Mid(t, i - 1, 1), " ", vbTextCompare) > 0 Then ' space found
      newS = newS & UCase(Mid(t, i, 1))
    Else
      newS = newS & LCase(Mid(t, i, 1))
    End If
    i = i + 1
  Loop  

  FindLabel = newS
End Function
  • Extract a number:
Code:
Function FindLabel ( [NAME] )
' Extracts number from [NAME]
' ---------------------------
  Dim sIn, sNew, i
  sIn = [NAME]  ' input field value
  sNew = ""  'result

  i = 1
  ' check if char is numeric
  While i <= Len(sIn)    
    If IsNumeric(Mid(sIn, i, 1)) Then
      sNew = sNew & (Mid(sIn, i, 1))
    End If
    i = i + 1
  Wend

  FindLabel = sNew
End Function
  • Stack a label in multiple lines:
Code:
Function FindLabel ( [NAME] )
' Stacks a label in multiple lines if
' exceeding a given length.
' Note: Not applicable to line layers.
' ------------------------------------
  dim s, sNew, i, l
  s = [NAME]
  l = Len(s)
  if l > 10 then  ' stack if exceeding this length
    sNew = Left(s,10)
    i = 11  ' scan for blank space starting from this position
    While i <= l
      if Mid(s,i,1) = " " then
        sNew = sNew & VBNewLine
      else
        sNew = sNew & Mid(s,i,1)
      end if
      i = i + 1
    Wend
  else
    sNew = s
  end if

  FindLabel = sNew
End Function
Note:
For more information, refer to the ArcGIS Desktop help topics: Labeling, Specifying text. 

Article ID:000004291

Software:
  • ArcMap 8 x
  • ArcMap 9 x

Get help from ArcGIS experts

Contact technical support

Download the Esri Support App

Go to download options

Related Information

Discover more on this topic