LAMBDA · Text

Format phone LAMBDA

xl.format.phone turns ten digits into (512) 555-0148. A leading US country code 1 is dropped. Anything else is returned unchanged.

Microsoft 365, Excel 2024, and Excel for the web. Excel 2021 does not have LAMBDA.

Syntax =xl.format.phone(phone)

xl.format.phone
=LAMBDA(phone,
  LET(
    cleaned, SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(phone, "(", ""), ")", ""), "-", ""), " ", ""), ".", ""), "+", ""),
    digits, IF(AND(LEN(cleaned)=11, LEFT(cleaned, 1)="1"), RIGHT(cleaned, 10), cleaned),
    IF(LEN(digits)=10,
      "(" & LEFT(digits, 3) & ") " & MID(digits, 4, 3) & "-" & RIGHT(digits, 4),
      phone)
  )
)

Arguments

NameRequiredWhat to pass
phoneRequiredA phone number as text. Digits, spaces, dots, hyphens, and parentheses are accepted. Pass a 10-digit value as text so Excel does not switch it to scientific notation.

Examples

An Austin number with hyphens

=xl.format.phone("512-555-0148")

Result (512) 555-0148

=xl.format.phone(B1)B2
Phone
AB
1Raw512-555-0148
2Formatted(512) 555-0148
A picture of the sheet, not a live calculator. 555-0100 through 555-0199 are reserved for fiction.

xl.format.phone is the display form of a US number. 512-555-0148, 5125550148, and 1 512 555 0148 all become (512) 555-0148. The 555 prefix here is the fictional range. A real sheet uses the same steps on a real number.

Store the source as text. A 10-digit number can survive as a number, but a longer value will lose digits past Excel’s 15-digit precision.

Add it to the workbook

Copy the formula in the block above, including the line breaks. Choose Formulas, Name Manager, New. Set the name to xl.format.phone, the scope to Workbook, and paste into Refers to. Excel Labs can do the same step: open the Advanced Formula Environment, add that name, and save it back to the workbook. A cell then calls =xl.format.phone(...).

The name needs Microsoft 365, Excel 2024, or Excel for the web. Excel 2021 returns #NAME? for LAMBDA even when it already has XLOOKUP, FILTER, or LET.

How it works

Six SUBSTITUTE calls remove parentheses, hyphens, spaces, dots, and plus signs. If 11 characters remain and the first is 1, the country code is dropped. Ten digits are then sliced into a 3-digit area code, a 3-digit exchange, and a 4-digit line. Any other length returns the original input instead of a half-formatted string.

Limits

Letters in a vanity number stay in the string and usually make the length fail the 10-digit test, so the original comes back. The function does not check that the area code is assigned. It only shapes digits.