Extract Proper Case words

webbug08

New Member
Joined
Aug 17, 2014
Messages
3
Hello All,

I have been using following code to extract all upper case words in a string but the problem is I can not extract words which are proper. For example

This is GOOD

Present output: GOOD
Desired Output: This GOOD

Code:
Function UpperWords(str As Variant) As String
    Dim i As Integer, sTemp As String, StrTmp As String
    For i = 0 To UBound(Split(str, " "))
        StrTmp = Split(str, " ")(i)
        If UCase(StrTmp) = StrTmp Then sTemp = sTemp & " " & StrTmp
    Next i
    UpperWords = Trim(sTemp)
End Function


Can any one please suggest what can be suitable modification in this case?

Thanks,
Webbug
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Try
Code:
Function ProperUPPERWords(str As Variant) As String
    Dim i As Integer, Words As Variant, StrTmp As String
    
    Words = Split(str, " ")
    
    For i = 0 To UBound(Words)
        StrTmp = Words(i)
        If LCase(StrTmp) = StrTmp Then
            Words(i) = vbNullString
        End If
    Next i
    ProperUPPERWords = WorksheetFunction.Trim(Join(Words, " "))
End Function
 
Upvote 0
This works very well but the only problem is that, the earlier function also reported the numbers in the string but this is not. Is there any work around it?

Thanks,
Webbug
Try
Code:
Function ProperUPPERWords(str As Variant) As String
    Dim i As Integer, Words As Variant, StrTmp As String
    
    Words = Split(str, " ")
    
    For i = 0 To UBound(Words)
        StrTmp = Words(i)
        If LCase(StrTmp) = StrTmp Then
            Words(i) = vbNullString
        End If
    Next i
    ProperUPPERWords = WorksheetFunction.Trim(Join(Words, " "))
End Function
 
Upvote 0
Try
Code:
Function ProperUPPERWords(str As Variant) As String
    Dim i As Integer, Words As Variant, StrTmp As String
    
    Words = Split(str, " ")
    
    For i = 0 To UBound(Words)
        StrTmp = Words(i)
        [COLOR=#ff0000][B]If LCase(StrTmp) = StrTmp Then[/B][/COLOR]
            Words(i) = vbNullString
        End If
    Next i
    ProperUPPERWords = WorksheetFunction.Trim(Join(Words, " "))
End Function
That test assumes perfect typing and won't catch words like ONe, TwO or thrEE. If such words could exist and if the OP would want them removed (as opposed to being left in so they could be seen and corrected), then this replacement code line could be used...
Code:
If StrTmp Like "[!A-Z]*" Or Mid(StrTmp, 2) Like "*[!a-z]*" Then

EDIT NOTE: Just saw the OP's latest posting (Message #3)... the above suggested replacement will handle numbers in the text.
 
Last edited:
Upvote 0
Rick, perhaps I've mis-understood but that doesn't produce the stated desired output.

I forgot about testing for all upper case letters.:oops: I think this replacement line will work...
Code:
If StrTmp Like "[!A-Z]*" Or (Mid(StrTmp, 2) Like "*[!a-z]*" And UCase(StrTmp) <> StrTmp) Then
 
Upvote 0
The test in my UDF could be changed
Code:
If Not ((UCase(StrTmp) = StrTmp Or StrConv(StrTmp, vbProperCase) = StrTmp) And Not (StrTmp Like "*[!A-za-z]*")) Then
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,550
Members
449,088
Latest member
davidcom

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top