Label expression by way of VBScript

Last Published: April 25, 2020


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.

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.


  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:
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))
      newS = newS & LCase(Mid(t, i, 1))
    End If
    i = i + 1

  FindLabel = newS
End Function
  • Extract a number:
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

  FindLabel = sNew
End Function
  • Stack a label in multiple lines:
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
        sNew = sNew & Mid(s,i,1)
      end if
      i = i + 1
    sNew = s
  end if

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

Article ID:000004291

  • ArcMap 8 x
  • ArcMap 9 x

Receive notifications and find solutions for new or common issues

Get summarized answers and video solutions from our new AI chatbot.

Download the Esri Support App

Related Information

Discover more on this topic

Get help from ArcGIS experts

Contact technical support

Download the Esri Support App

Go to download options