Search a string for paragraph marks

G

GrannyM

I have a form with a multi-line textbox. After the user has entered in their
text, I need to know how many returns they've entered. I've been using text
with 5 lines (each with a hard return) for testing purposes, but have not
been able to find code that will tell me the user entered 5 hard returns.
Here is what I've tried and the values I get:

NumPara = InStr(1, searchtext$, chr(10)) - returns 30
NumPara = InStr(1, searchtext$, chr(11)) - returns 0
NumPara = InStr(1, searchtext$, chr(13)) - returns 29
NumPara = InStr(1, searchtext$, vbLf) - returns 30
NumPara = InStr(1, searchtext$, vbcr) - returns 29
NumPara = InStr(1, searchtext$, "^p") - returns 0

I've also tried "^11", "^13", "^10", and all return 0. I've tried
combining chr(13) & chr(10) and vbcrlf.

Anyone have any other ideas?
 
G

Graham Mayor

Try Plan B

Dim sText As Range
Dim aPara As Variant
Set sText = Selection.Range
aPara = sText.Paragraphs.Count
MsgBox aPara 'Returns 5


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
J

Jonathan West

GrannyM said:
I have a form with a multi-line textbox. After the user has entered in
their
text, I need to know how many returns they've entered. I've been using
text
with 5 lines (each with a hard return) for testing purposes, but have not
been able to find code that will tell me the user entered 5 hard returns.
Here is what I've tried and the values I get:

NumPara = InStr(1, searchtext$, chr(10)) - returns 30
NumPara = InStr(1, searchtext$, chr(11)) - returns 0
NumPara = InStr(1, searchtext$, chr(13)) - returns 29
NumPara = InStr(1, searchtext$, vbLf) - returns 30
NumPara = InStr(1, searchtext$, vbcr) - returns 29
NumPara = InStr(1, searchtext$, "^p") - returns 0

I've also tried "^11", "^13", "^10", and all return 0. I've tried
combining chr(13) & chr(10) and vbcrlf.

Anyone have any other ideas?

InStr returns the character position of the first occurrance of the search
string. having found the first occurrance, you have to run the search again
starting at the next character, and repeat until InStr returns 0

Something like this

Function NumInclusions(sSearch, sIn) As Long
Dim iTotal as Long
Dim iPos as Long

Do
iPos = InStr(iPos + 1, sSearch, sIn)
If iPos = 0 Then Exit Do
iTotal = iTotal + 1
Loop
NumInclusions = iTotal
End Function
 
H

Helmut Weber

Hi Granny,

here and now, in a multiline textbox on a userform
the line feed is chr(13) & chr(10).

You may use:
MsgBox UBound(Split(TextBox1.Text, Chr(13) & Chr(10))) + 1
as split returns a zero based array.
 
G

GrannyM

Thanks! this worked great, although I've never used anything like this
before. It must be more advanced then anything I've worked with.

Jonathan's loop also worked, so Thanks to you both! I had forgotten that
InStr returns the position, not the number of occurances.
 

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