We help IT Professionals succeed at work.
Get Started

macro to scroll through words selecting and replacing text until...

DrTribos asked
Last Modified: 2012-05-06
Hi Experts

I'm trying to correct units in a document.

Basically I want to replace:
"meters per second" with "m/s"
"l/minute" with "L/min"

In the first example (m/s) I look at the word after my number (number selected by other code) and if it is in the array I replace it with the correct unit (arrays have corresponding entries: i.e. if element 5 in findUnit is meters then element 5 in repUnit is also 5). So I find meters --> replace with m, find per --> replace with /, find second --> replace with s THEN find the NEXT word and determine that it is not a unit so GoTo FindNextNum.

I'm ok to find the first word after the number but get confused trying to find subsequent words... dealing with "/" if the oringal text was meters/second --> would need to become m/s and dealing with:

non-breaking space,
paragraph and end-of-cell markers...

An additional requirement is that there are no spaces in the final product.

findUnit and repUnit are Arrays
unitRng is a range

Many thanks, Steve
    For i = LBound(findUnit) To UBound(findUnit)
        If Trim(unitRng.Text) = findUnit(i) Then
            MsgBox (findUnit(i) & " will be replaced by " & repUnit(i))
            Set nUnitRng = unitRng
            nUnitRng.Next(unit:=wdWord, Count:=1).Select
            Set nUnitRng = Selection.Range
            MsgBox ("Next Unit Preselected")
            Selection.TypeText (repUnit(i))
Set unitRng = nUnitRng
            MsgBox ("Next Unit")
            GoTo FindNextUnit
        End If
    Next i

Open in new window

Watch Question
Top Expert 2012
This problem has been solved!
Unlock 3 Answers and 21 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE