-2

IN ArcGIS, what is the vbscript in the label expression box to build a label which has the outcome that if the string is over 50 characters long then the label should be formatted over 2 lines.

  • 1
    Welcome to GIS SE! What have you tried? Please edit your question to include what you've tried, and detail what happens when you try it. Which GIS software are you using? – Midavalo Feb 24 '17 at 0:18
  • If you are using ArcMap, are you open to using the Python Parser instead? Whether it is available would depend on your version. – PolyGeo Feb 24 '17 at 0:22
  • I'm open to using the Python Parser. – Marc B Feb 24 '17 at 1:01
2

This is doable using the VBS label parser in ArcGIS, although recommended you start looking at using the Python parser as VBA/VBS is being phased out of ArcGIS Products.

In the Label Expression dialog check the Advanced box, and then paste the code below. Change [Description] to the fieldname you are wanting to label.

If you just want to split the text after the 50th character you can use the following, however it may split mid-word and ignores whether the second line is longer than 50 characters also.

Function FindLabel ([Description])
  If len([Description]) <= 50 Then
    FindLabel = [Description]
  Else
    FindLabel = left([Description], 50) & vbNewLine & mid([Description], 50)
  End If
End Function

The following splits the label every 50 characters, at the nearest space (so doesn't split words), and splits additional lines if they are also longer than 50 characters.

Function FindLabel ([Description])
  If len([Description]) <= 50 Then
    FindLabel = [Description]
  Else
    newString = ""
    subString = ""
    a = Split([Description])
    For Each x in a
      If subString = "" Then
        subString = x 
      Else  
        lenSubString = len(subString & " " & x)
        If lenSubString > 50 Then
          If newString = "" Then
            newString = subString
            subString = ""
          Else
            newLine = True
            newString = newString & vbNewLine & subString
            subString = ""
          End If
        Else
          newLine = False
          subString = subString & " " & x
        End If
      End If
    Next
    FindLabel = newString
  End If
End Function
  • Thank you for your help but I have an error message of "Carriage returns are not allowed in simple expressions" – Marc B Feb 24 '17 at 1:09
  • @MarcB I've fixed my answer - In the Label Expression dialog check the Advanced box before pasting the code – Midavalo Feb 24 '17 at 1:10
  • Midavalo, I cannot thank you enough. It works, fantastic. – Marc B Feb 24 '17 at 1:34
6

This is why you want Python:

def FindLabel ( [TEXT]):
  t= [TEXT] 
  return '%s\n%s' %(t[:50],t[50:])

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.