Troubleshooting VBScript for labeling in ArcMap?

I am trying to add FIPS codes to my county name labels and want to add either one or two zeros to the number unless it is over 100. i'm hoping someone can find the error in my code:

Function FindLabel ([Name], [FIPS]) if ([FIPS] < 10) then FindLabel = [Name] & vbNewLine & FormatNumber (00,[FIPS]) else if (9 < [FIPS] > 100) then FindLabel = [Name] & vbNewLine & FormatNumber (0,[FIPS]) else FindLabel = [Name] & vbNewLine & [FIPS] end if End Function

Unless you need to use vbscript, you can accomplish this in one line of python:

{1:03d}".format([Name], int([FIPS]))


The curly braces reference the index position of the parameters passed to.format(). is your newline character and the:03dbit in the second one zero pads the input so that the result is of length 3 (assuming the number isn't 3 digits already--in which case the whole number is returned.)

>>> for i in (1,7,33,98,200,123456): print("{0} becomes {1:03d}".format(i, i)) 1 becomes 001 7 becomes 007 33 becomes 033 98 becomes 098 200 becomes 200 123456 becomes 123456

If you want to use vbscript, here's an amusing approach that abusesRight():

Function FindLabel ([Name], [FIPS]) if [FIPS] < 1000 then FindLabel = [Name] & vbNewLine & Right("00"&[FIPS], 3) else FindLabel = [Name] & vbNewLine & [FIPS] end if End Function

I believe the VB should look like this, but Paul's solution more elegant. This takes advantages of VB string formatting to force any number to be four digits (padding with leading zeroes if necessary).

Function FindLabel ([Name], [FIPS]) FindLabel = [NAME] & vbNewLine & Format$([FIPS],"0000") End Function

Watch the video: Annotation In ArcGIS (September 2021).