I have a Word macro that I got working with a lot of help from a few forum members for which I am very thankful.
Now, due to some changes at work, I have to switch to using Excel 2010 rather than Word. Could someone change the following Word code to work in Excel 201?
In the Word document the target text was copied and pasted and the macro worked on it while it was still selected. In the new Excel file, the names will be in an Excel cell so it should be easier to work with.
Code:
Function ProperCase(strOneLine As String, intChangeType As Integer) As String
'---------------------------------------------------------------
'- This function will convert a string to Proper Case -
'- The initial letter of each word is capitalised. -
'- It will also handle special names such as O', Mc and -
'- hyphenated names -
'- if intChangeType = 1, all text is converted to proper case. -
'- e.g. 'FRED' is converted to 'Fred' -
'- if intChangeType = 0, upper case text is not converted. -
'- e.g. 'fred' becomes 'Fred', but 'FRED' remains unchanged. -
'---------------------------------------------------------------
Dim I As Integer
Dim bChangeFlag As Boolean
Dim strResult As String
'----------------------------------------------------------
'- No characters in string - nothing to do -
'----------------------------------------------------------
If Len(strOneLine) = 0 Then
ProperCase = ""
Exit Function
End If
'----------------------------------------------------------
'- Always set first letter to upper case -
'----------------------------------------------------------
strResult = UCase$(Left$(strOneLine, 1))
'----------------------------------------------------------
'- Now look at the rest of the string -
'----------------------------------------------------------
For I = 2 To Len(strOneLine)
'----------------------------------------------------------
'- If the previous letter triggered a capital, change -
'- this letter to upper case -
'----------------------------------------------------------
If bChangeFlag = True Then
strResult = strResult & UCase$(Mid$(strOneLine, I, 1))
bChangeFlag = False
'----------------------------------------------------------
'- In other cases change letter to lower case if required -
'----------------------------------------------------------
Else
If intChangeType = 1 Then
strResult = strResult & LCase$(Mid$(strOneLine, I, 1))
Else
strResult = strResult & Mid$(strOneLine, I, 1)
End If
End If
'----------------------------------------------------------
'- Set change flag if a space, apostrophe or hyphen found -
'----------------------------------------------------------
Select Case Mid$(strOneLine, I, 1)
Case " ", "'", "-", "’" 'the last quote is done by holding ALT and typing 0146. True curly quotes!!
bChangeFlag = True
Case Else
bChangeFlag = False
End Select
Next I
'----------------------------------------------------------
'- Special handling for Mc at start of a name -
'----------------------------------------------------------
If Left$(strResult, 2) = "Mc" Then
Mid$(strResult, 3, 1) = UCase$(Mid$(strResult, 3, 1))
End If
I = InStr(strResult, " Mc")
If I > 0 Then
Mid$(strResult, I + 3, 1) = UCase$(Mid$(strResult, I + 3, 1))
End If
'----------------------------------------------------------
'- Special handling for Mac at start of a name -
'----------------------------------------------------------
If Left$(strResult, 3) = "Mac" Then
If Len(Split(Trim(strResult), " ")(0)) > 5 Then
Mid$(strResult, 4, 1) = UCase$(Mid$(strResult, 4, 1))
End If
End If
I = InStr(strResult, " Mac")
If I > 0 Then
If Len(strResult) > I + 5 Then
Mid$(strResult, I + 4, 1) = UCase$(Mid$(strResult, I + 4, 1))
End If
End If
ProperCase = strResult
End Function
Thanks for your help!!