Visual Basic Code of the Week (COTW)
Issue #130
Online Version at (paid subscribers only)
All content and source code is Copyright (c) 2000 by C&D Programming Corp. No part of this issue can be reprinted or distributed in any manner without express written permission of C&D Programming Corp.

Issue topic: A very useful phone number formatter

Extra CASH! and a new Newsletter

We are going to create a new VB Tips newsletter. There will be no fee for this newsletter. It will contain short (10-50 lines of text) code snippets, tips, and other useful information for Visual Basic. If you have any tips to contribute, email us at Be sure to include instructions and source code. For each tip received which gets published we will pay you $10 to $50 US Dollars depending on length and complexity.

If you are interested in subscribing to this newsletter just send an email to and you will be added to our subscriber list.


In this Issue

This issue presents one method to format a United States phone number. It will also work in locations where the phone numbers are 7 digits or 10 digits long. It can easily be modified to fit your local phone number requirements.

If you have any questions about using this module, let us know at


This module shows a good model for formatting phone numbers (or other numeric values). It will format a 7 digit or 10 digit phone number in the format 999-9999 or (999)999-9999.


Public Function FormatUSPhoneNumber(ByVal sPhone As String) As String

Call this routine with a phone number as the specified parameter. It will raise an error if the phone number is not 7 or 10 digits after all non-numerics are removed. It will return a string in the format of 999-9999 or (999)999-9999. If you want to modify the formatted output update the Format$ line in the routine.

Sample Usage

This sample shows how to use the FormatUSPhoneNumber function.

    Debug.Print FormatUSPhoneNumber("2125047945")
    ' the output will be: (212)504-7945

    Debug.Print FormatUSPhoneNumber("212-504-7945")
    ' the output will be: (212)504-7945

Source Code

To see the source code for this issue you must be a subscriber to Code of the Week. If you are a subscriber the source code is available at the following address:

This document is available on the web

Paid subscribers can view this issue in HTML format. There is no additional source or information in the HTML formatted document. It just looks a little better since we have included some HTML formatting. Just point your browser to link at the top of this document.

Other links

Contact Information

C&D Programming Corp.
PO Box 20128
Floral Park, NY 11002-0128
Phone or Fax: (212) 504-7945