Home arrow GIS Data & Resources arrow Scripts and Code arrow ArcMap Label & .CAL arrow VB Script Label Expression for Multiple Line Labels in ArcMap
VB Script Label Expression for Multiple Line Labels in ArcMap PDF Print E-mail

Written by Bert Granberg,

This label expression can be used to display long text strings from an attribute field as a multi-line label. Basically it attempts to add hard returns (line breaks) after every other word.

For example, the script will display the value "Western Washington County Special Service District 1, Pine Valley" as:

Western
Washington
County Special
Service District 1,
Pine Valley

There are three parameters to keep in mind.

  • The input field name, currently set to [Name] in the code below. Change this in two places to the field from which you would like to get the label string.
  • MaxWordLength, currently set to 10, change this to force long words onto their own line like 'Washington' in the example above
  • MixWordLength, currently set to 3, change this to keep short words from being put on the next line like '1,'  in the example above

When using a label expression, you'll need to click the expression button on the label properties window and then click the advanced button. Delete the suggested code and replace it with the code below.

 

Function FindLabel ( [Name] )

    Dim strArray
    Dim outStr
    Dim x
    Dim maxWordLength
    Dim minLengthLength
    Dim everyother
    Dim isFirst
    Dim lastWordLong
    lastWordLong = false
    
    maxWordLength = 10
    minWordLength = 3
    everyother = False
    isFirst = True
    strArray = Split( [Name], " ")
    
    For x = 0 To UBound(strArray)
    
        If ((Not isFirst) And (Len(strArray(x)) <= minWordLength)) Then
            'keep short word on existing line
            outStr = outStr & " " & strArray(x)
            everyother = False
        ElseIf (everyother) Then
            'word x is slated to be second word on line
            If (Len(strArray(x)) > maxWordLength) Then
                outStr = outStr & vbNewLine & strArray(x)
                everyother = False
            Else
                outStr = outStr & " " & strArray(x)
                everyother = False
            End If
        Else
            'word x is slated to be first word on line
            If (isFirst) Then
                outStr = strArray(x)
                everyother = True
                isFirst = False
            Else
                If (Len(strArray(x)) > maxWordLength) Then
                    lastWordLong = True
                    everyother = False
                Else
                    lastWordLong = False
                    everyother = True
                End If
                
                outStr = outStr & vbNewLine & strArray(x)

            End If
        End If
    Next

    FindLabel = outStr
End Function


Users' Comments  
 

No comment posted

Add your comment

31, Jul. 2008
Last Updated ( 31, Jul. 2008 )
 
< Prev   Next >

AGRC Contacts | UGIC Contacts

feed image feed image

Utah GIS Portal © 2009 AGRC

Optimized for