That is not a name I would ever expect, but given the OP's latest posts, it might be one that he could expect. The fix to my code to handle that is simple, though...Rick,
One possible problem that may occur with your code for Hyphenated name like "John V-DAMON".
Function LastName(S As String) As String
Dim X As Long
For X = 1 To Len(S)
If Mid(S & " ", X, 2) Like "[A-Z][A-Z' -]*" Then
If Mid(S, X) = UCase(Mid(S, X)) Then
LastName = Trim(Mid(S, X))
Exit For
End If
End If
Next
End Function
That is not a name I would ever expect, but given the OP's latest posts, it might be one that he could expect. The fix to my code to handle that is simple, though...
Note to Ombir: I noticed you appeared to be already looking at my code in Message #60 while I was posting an edited version of the original code I posted there. The above code without the dash in the first If..Then statement is the edited version of the code I think you actually saw (which also did not handle V-DAMON as a last name, hence the code above).Code:Function LastName(S As String) As String Dim X As Long For X = 1 To Len(S) If Mid(S & " ", X, 2) Like "[A-Z][A-Z' -]*" Then If Mid(S, X) = UCase(Mid(S, X)) Then LastName = Trim(Mid(S, X)) Exit For End If End If Next End Function
I don't understand... you say you "don't get the data via a file", but then you say you say that you "currently paste it in manually using the text import wizard". Where exactly is the data coming from? And can you post some examples of what that data actually looks like?Is there a way to use a macro to paste in a comma delimited subset of data. I can get it to work using a text file but at the minute I don't get the data via a file.
I currently paste it in manually using the text import wizard as a delimited data with comma separation. Can someone provide the VBA for pasting this in without the manual text import wizard
On Error Resume Next
Range("A2:F18").Select
Selection.ClearContents
Range("A2").Select
ActiveSheet.Paste
Selection.TextToColumns DataType:=xlDelimited, _
ConsecutiveDelimiter:=True, Comma:=True
On Error GoTo 0
This is the UDF (user defined function) that I came up with...
Code:Function UpperCaseWords(S As String) As String Dim X As Long, TempText As String TempText = " " & S & " " For X = 2 To Len(TempText) - 1 If Mid(TempText, X, 1) Like "[!A-Z ]" Or Mid(TempText, X - 1, 3) Like "[!A-Z][A-Z][!A-Z]" Then Mid(TempText, X) = " " End If Next UpperCaseWords = Application.Trim(TempText) End Function
HOW TO INSTALL UDFs
------------------------------------
If you are new to UDFs, they are easy to install and use. To install it, simply press ALT+F11 to go into the VB editor and, once there, click Insert/Module on its menu bar, then copy/paste the above code into the code window that just opened up. That's it.... you are done. You can now use UpperCaseWords just like it was a built-in Excel function. For example,
=UpperCaseWords(A1)
Living in the US my whole life, I have never had to deal with UTF-8 characters, so I am sorry, but I do not know how to fix it to work with them.Rick, thank you for this formula! It works like a charm with English characters. But it fails with UTF-8 characters.
For example:
If I have a string "ĄŽUOLAS STELMUŽĖ Yra Vienas stipriausių", then your formula returns "UOLAS STELMU".
But it should return "ĄŽUOLAS STELMUŽĖ".
How to fix formula so it would recognize UTF-8 characters too?
Sub Test()
Range("A2").Value = KeepUCaseW(Range("A1").Value)
End Sub
Function KeepUCaseW(s As String) As String
Dim v As Variant
Dim j As Long
v = Split(s)
For j = 0 To UBound(v)
If v(j) <> UCase(v(j)) Then v(j) = ""
Next j
KeepUCaseW = Application.Trim(Join(v))
End Function
That is not a name I would ever expect, but given the OP's latest posts, it might be one that he could expect. The fix to my code to handle that is simple, though...
Note to Ombir: I noticed you appeared to be already looking at my code in Message #60 while I was posting an edited version of the original code I posted there. The above code without the dash in the first If..Then statement is the edited version of the code I think you actually saw (which also did not handle V-DAMON as a last name, hence the code above).Code:Function LastName(S As String) As String Dim X As Long For X = 1 To Len(S) If Mid(S & " ", X, 2) Like "[A-Z][A-Z' -]*" Then If Mid(S, X) = UCase(Mid(S, X)) Then LastName = Trim(Mid(S, X)) Exit For End If End If Next End Function