How to put dashes (-) after every third number?

Posted on 2012-08-23
Last Modified: 2012-08-25
I have documents that consists of a table on each page where I have to type in numbers in this format (###-###-###). I would like to just type in the numbers and then run a macro that would automatically enter the dashes.

I would like the macro to run from a command button on the right side of the first page but it can't print when the page is printed if that is possible!

I've attached a copy of the file I use to enter the numbers.

Thanks in advance for any help!
Question by:calbais
    LVL 76

    Expert Comment

    You could do a WildCard Find and Replace

    Find: <([0-9]{3})([0-9]{3})([0-9]{3})>
    Replace: \1-\2-\3

    This is a good article on Word wildcards:
    LVL 6

    Expert Comment

    by:Let's Go
    Alternatively, you could embed an Excel spreadsheet in the Word document (or just use Excel) and set the number format in Excel.

    To do this in Word 2010, click on the arrow next to Insert Table, then choose Excel Spreadsheet.
    Inserting an Excel spreadsheet
    Then select the relevant cells in the (Excel) table, right-click and choose Format, select Custom and type in the format as 000-000-000.
    Setting the number format
    The numbers will then automatically be displayed in your desired format.
    LVL 76

    Accepted Solution

    If you require a macro, this will do it:
    Sub FormatFileNumbers()
        With ActiveDocument.Range.Find
            .MatchWildcards = True
            .Text = "<([0-9]{3})([0-9]{3})([0-9]{3})>"
            .Replacement.Text = "\1-\2-\3"
            .Execute Replace:=wdReplaceAll
        End With
    End Sub

    Open in new window

    However, don't put a button on a document, especially one like yours. It is multi-page, so the button would not always be visible. You could create a QAT button or a command button. Or you could make a keystroke shortcut.
    You need to report the Word version for further details.

    Author Comment

    Pfsnet... I would love to use Excel since I'm more familiar with it but I have no control over that at work. We have to use the Word document they provide. Thanks though!

    GrahamSkan... I'm going to give your macro idea a try at work today. That looks like what I want. I'm using Word 2010. I'll put a button on the ribbon to activate the macro.
    Thanks for your suggestion!

    Author Comment

    Thanks GrahamSkan. That macro works perfectly!!

    I put a button on the ribbon so it would always be available to click but when I gave the document to a co-worker, the button was missing. Do you know if there is a way to make the button stay active when the document is distributed to someone else?


    LVL 76

    Expert Comment

    You would have to put the code and the keystroke in the document - or in its attached template which would also have to be distributed to your colleague.

    Author Comment

    Yeah, I did that but it didn't work!

    Anyway, I'll figure it out. You answered my question here so here's your points.

    Thanks for your help.



    Author Closing Comment

    GrahamSkan answered quickly  and accurately. His solution worked perfectly the first time!!

    Thanks very much!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Introduction Authors who set out to write any sort of lengthy piece for online submission—be it a long question or comment on a technical form, an article, or a substantial blog entry—often find it useful to work up a draft in an editor other t…
    Introduction This tutorial provides instructions on how to properly format your Word document using the inbuilt tools provided. The benefits of using these tools means your documents are more accessible and easily portable to other applications an…
    This video walks the viewer through the process of creating an MLA formatted document, as well as a bibliography with citations.
    This Micro Tutorial well show you how to find and replace special characters in Microsoft Word. This is similar to carriage returns to convert columns of values from Microsoft Excel into comma separated lists.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now