Results 1 to 4 of 4
  1. #1
    Lounger
    Join Date
    Jan 2001
    Location
    Missouri, USA
    Posts
    41
    Thanks
    0
    Thanked 0 Times in 0 Posts

    hopefully easy string search (VBA - Word 2000)

    I'm trying to standardize some documents. One factor I need to standardize is stored in variable strPresentationNumber. This should contain values such as "ABC-123" but many are missing the hyphen: "ABC123". (Note that the length of each half of the string is variable but there should always be a hyphen between the alphas and numbers.)

    I was going to search for a hyphen. If not found, I would then find the first digit and put a hyphen in front of it. Unfortunately, I don't know how to do this search. Can anyone help? Or suggest a more efficient way to do this?

    TIA

  2. #2
    2 Star Lounger
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    199
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Re: hopefully easy string search (VBA - Word 2000)

    Tekvet,

    I think one way you can do this with a three stage search and replace.

    1. Search for "Any Digit" and "Any Letter" and in the Replace box use "Find What Text" followed by some unique character string, such as "###". This will identify where the dash is missing.

    2. Search for "Any Letter" and "###" and Replace with "-" and "Find What Text". This inserts the dash in front of the letter, and after the digit.

    3. Search for "###" and Replace with nothing - use a backspace if necessary. This will remove the hashes and give you what you want.

    I would be pleased to see if someone can do it in one search and replace.

    Hope this helps.

  3. #3
    Uranium Lounger
    Join Date
    Jan 2001
    Location
    South Carolina, USA
    Posts
    7,295
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: hopefully easy string search (VBA - Word 2000)

    Something like this should do what you want:

    <pre> If InStr(strPresentationNumber, "-") = 0 Then
    For I = 1 To Len(strPresentationNumber)
    If IsNumeric(Mid(strPresentationNumber, I, 1)) Then
    Exit For
    End If
    Next I
    strPresentationNumber = Left(strPresentationNumber, I - 1) & "-" & _
    Right(strPresentationNumber, Len(strPresentationNumber) - I + 1)
    End If
    </pre>

    Legare Coleman

  4. #4
    Lounger
    Join Date
    Jan 2001
    Location
    Missouri, USA
    Posts
    41
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: hopefully easy string search (VBA - Word 2000)

    Awesome -- that worked perfectly! <img src=/S/joy.gif border=0 alt=joy width=23 height=23>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •