You're getting there. Still a problem with returning extra spaces -- if
there
are multiple spaces after the CapWord, your routine returns them, also.
It seems that what is happening is that if there are multiple, sequential
spaces, the Split function returns a null string ("") for the spaces after
the
first. Since "" = Ucase(""), the null string gets concatenated, followed
by
your Delimiter.
Yes, the blanks do cause a problem because the test I was doing was too
simple. All I checked for was that the word equaled its capitalization.
Well, as it turns out, an empty string equals the empty string with the
UCase function applied and so it passed the test. Actually, in examining
this problem, another problem was highlighted... if there is stand-alone
non-letters, these too passed my simple test; hence, something like "(*)"
was added to the return value. This part is easy enough to fix and the code
to do that is shown after my signature below. However, there is another
difference between our codes and I am not sure which of our treatments
should be considered the "correct" one. If the string of text is this, for
example...
"One (TWO) Three"
my routine returns (TWO) with the parentheses and your routine returns just
TWO. The OP said in his Subject line as well as the body of his first
posting that the items he wanted to find were "words"... so, should the
surrounding parentheses be retained as I do (because they are attached to
the upper case letters and/or digits) or removed as you do (even though they
are attached)? Or are we both wrong and the "word" should be rejected
altogether because it is not composed of just upper case letters and/or
digits? We will need input from the OP on this issue.
Now, while testing the above, I came across a problem with your function. If
there are any internal non-letters/non-digits, your regular expression
appears to treat them as blanks and separates the word into parts, using a
blank, for each such character. So, for this sentence...
"One TWENTY-ONE Three"
your routine returns TWENTY ONE with a space in the middle. Your code does a
similar thing with something like...
"Model #AB(12)CD"
returning AB CD EF with two internal blanks.
Thank you... I did.
We just had a wonderful one. My mother-in-law and niece
are visiting from the Azores, and they go home tomorrow. My wife and
niece
made a great "going away" meal, and some of our kids were over, too.
Yes, that sounds like it was nice too. I glad you had such a nice time.
Rick
'The repaired function
'==============================
Function GetCapWords(R As Range) As String
Dim X As Long
Dim Words() As String
Const Delimiter = " "
Words = Split(Replace(Replace(R.Value, vbLf, " "), vbCr, " "))
For X = 0 To UBound(Words)
If Words(X) = UCase(Words(X)) And Words(X) Like "*[A-Z]*" Then
GetCapWords = GetCapWords & Delimiter & Words(X)
End If
Next
If Len(GetCapWords) > 0 Then
GetCapWords = Mid(GetCapWords, 1 + Len(Delimiter))
End If
End Function