replace any combination of chr(13) with white space with chr13)

R

RB Smissaert

How do I do the following with VBA:
I want to replace any combination of:
chr(13), chr(9), chr(32) with a single chr(13)

Basically, any combination of whitespace with any number of linebreaks to
replace with a single linebreak.
I prefer to do this on a string rather than in the document.

Thanks for any advice.


RBS
 
H

Helmut Weber

Hi,
first, chr(13) is a paragraph mark,
not a linebreak. The following should remove
all chr(9) and chr(32) near (before or after)
a paragraph mark. Leaving lots of ways for improvement.
And use longer names for variables than I did here.
The reason is I am working on docs in galley view.
Leaving me just a small column for the VBE.
---
Function WhiteSpace(s As String, r As Variant) As Boolean
'r is the array of replacments to be done
Dim i As Integer
WhiteSpace = False
For i = 0 To UBound(r)
If InStr(s, r(i)) Then
WhiteSpace = True
Exit Function
End If
Next
End Function
'---
Sub test776()
Dim s As String ' the string from the selection
Dim f As Boolean ' found whitespace in string
Dim r(4) As String ' 4 cases (0 -3) of whitespace
' preceeding or following
' paragraph mark plus resulting
' double paragraph marks
Dim i As Integer ' just a counter
r(0) = Chr(9) & Chr(13)
r(1) = Chr(32) & Chr(13)
r(2) = Chr(13) & Chr(32)
r(3) = Chr(13) & Chr(9)
r(4) = Chr(13) & Chr(13)
s = Selection.Range.Text
f = WhiteSpace(s, r)
Do Until f = False
For i = 0 To UBound(r)
s = Replace(s, r(i), Chr(13))
Next
f = WhiteSpace(s, r)
MsgBox s ' for testing only
Loop
Selection.Range.Text = s
End Sub
 

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