I have a shapefile with a field I am using for labels. The attributes in this field are long company names. I will be publishing the contents of this map to ArcGIS Portal as a map service too, so I want to clean up my labels.
First, I'd like to stack the labels.
Second, I would like to truncate them by replacing common words in all these company names (eg. replace Company with "Co.", Beverage with "Bev.", Distributing with "Dist.", and so on...)
Is there a single script where I can accomplish both?
Thanks!
Function FindLabel([COMPANY_NAMES]) Dim MyArray, i, names MyArray = Split([COMPANY_NAMES], ",") For i = 0 to UBound(MyArray) If i = 0 Then names = MyArray(i) ' Begin a single company. Else names = names & vbCrLf & MyArray(i) ' Insert a new line after every company name. End If Next Replace(names, "Limited Liability Company", "LLC") Replace(names, "Company", "Co") Replace(names, "Beverage", "Bev") Replace(names, "Distributed", "Dist") FindLabel = names End Function
Hi rfairhur24 - thanks for the post reply.
I tried to copy and paste the code into the expression window under the Labels tab, but I get an error message:
"The expression contains an error.... Error 8 on line 35. Cannot use parentheses when calling a Sub."
Also, just to be clear, the field I'm using for the labels looks something like this:
[ATTACH=CONFIG]26782[/ATTACH]
Thanks so much for your input!
Function FindLabel([dist_name]) Dim name If IsNull([dist_name]) Then name = "" Else ' Add as many replace expressions as you need. ' The first string in the pair must match your input exactly to be replaced. ' The order of replace statements matters. ' Words that can be part of other replaced words must come last. name = [dist_name] name = Replace(name, "Limited Liability Company", "LLC") name = Replace(name, "Company", "Co") name = Replace(name, "Beverage", "Bev") name = Replace(name, "Distributed", "Dist") End if FindLabel = name End Function
Edit: I found the error in my code. I have to assign the replace output back to the name variable as shown below. Normally I use this in the Field Calculator and don't have to do that step, but it is required in a label expression.
Just for the sake of trying it, use this code. It will do nothing to stack your labels, but it should do the word replacements without error:Function FindLabel([dist_name]) Dim name If IsNull([dist_name]) Then name = "" Else ' Add as many replace expressions as you need. ' The first string in the pair must match your input exactly to be replaced. ' The order of replace statements matters. ' Words that can be part of other replaced words must come last. name = [dist_name] name = Replace(name, "Limited Liability Company", "LLC") name = Replace(name, "Company", "Co") name = Replace(name, "Beverage", "Bev") name = Replace(name, "Distributed", "Dist") End if FindLabel = name End Function
If you did the edits to the fields with the field calculator you would use the Replace expression anyway, i.e.:
Replace([dist_name], "Company", "Co")
So that statement will work unless there is something very strange in your field that I have never encountered before (none of the characters you mentioned would be special or trigger an error).
The only character that would throw an error in your company names that I know of is a double quote ("). Do you have that character in any actual company name?