Finding string of many spaces

E

Ed

I need to find strings of multiple space characters and reduce them to 0, 1,
or 2 spaces, depending on the first character. What I think I need to do is
find two consecutive spaces, then extend the range forward and backward
until I hit a non-space character. What do I use to do that?

Ed
 
A

Anne Troy

I just use Find and Replace. Find 3 spaces, replace with 1. Keep doing it
until zero replacements have been made. Then do it with 2 spaces...
If it depends on the first character, you can find, for instance,
period-space-space and replace with period-space.
************
Anne Troy
www.OfficeArticles.com
 
E

Ed

Thanks, Anne, but I'm not sure that approach would work here. I would wind
up doing multiple Finds, and some of what I would look for is no longer
valid because all the other spaces have been removed. I'm dealing with text
reports formatted by a old reports generator, which takes its information
from an even-older database. Also, our style requires two spaces after
certain characters, one space between words, and then I have to find extra
spaces where the reports generator broke a word and moved the rest to the
next line. Some of it's going to be a manual search-and-destroy, but I
wanted to automate as much as possible.

Ed
 
G

Greg

Ed,

Here is an example that might meet your needs.

Sub Test()
Dim oRng As Range
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = "[! ] {2,}[! ]"
.MatchWildcards = True
While .Execute
Select Case oRng.Characters.First.Text
Case Is = "."
oRng.Text = oRng.Characters.First & " " & oRng.Characters.Last
Case Is = ":"
oRng.Text = oRng.Characters.First & " " & oRng.Characters.Last
Case Is = "-"
oRng.Text = oRng.Characters.First & "" & oRng.Characters.Last
Case Else
'Do Nothing
End Select
Wend
End With
End Sub
 
G

Greg

Ed,

Missed a line in the earlier macro. Need to collapse the range.

Sub Test()
Dim oRng As Range
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = "[! ] {2,}[! ]"
.MatchWildcards = True
While .Execute
Select Case oRng.Characters.First.Text
Case Is = "."
oRng.Text = oRng.Characters.First & " " & oRng.Characters.Last
Case Is = ":"
oRng.Text = oRng.Characters.First & " " & oRng.Characters.Last
Case Is = "-"
oRng.Text = oRng.Characters.First & "" & oRng.Characters.Last
Case Else
'Do Nothing
End Select
oRng.Collapse Direction:=wdCollapseEnd
Wend
End With
End Sub
 
T

Tony Jollans

You don't really need VBA (but you can always record yourself if you want
some).

For example to replace a, c, or f, followed by multiple spaces with
(whatever the character was) followed by a single space:

Find/Replace

Find : ([acf]) {2,} - that's let parenthesis, left bracket, a, c, f, right
bracket, right parenthesis, space, left brace, two, comma, right brace

Replace: \1 - that's backslash, one, space

Check Use Wildcards
Replace All

Repeat for each different number of spaces following whatever characters you
want to check - you just need to make sure the characters (a, c and f in the
above example) are in order.
 
E

Ed

Wow! Thanks, Greg. I had tried something using wildcards, but couldn't get
it to work. Gotta learn more about that! I appreciate the boost.

Ed
 
E

Ed

Greg, it works great! I had to make one minor adjustment. After collapsing
the range, I added
oRng.MoveEnd Unit:=wdCharacter, Count:=-1
In a situation like
1 2 3 4
the code would find
1 2
and change it to
1 2
and collapse to after 2. That leaves a space as the start of oRng, and
because the search string begins with [! ] it would not find 2 3.
Instead, it would skip that and find 3 4, leaving me with
1 2 3 4. The MoveEnd fixed that.

Thanks again for your help.
Ed
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top