Capitalise all Words in VBA

As part of an Access data cleanse, I needed to capitalise company’s and people’s names and addresse. So I googled for a regex that would do it. Of course I found plenty that would work very nicely in other languages and on other platforms, but none that worked in VBA. I found that I was not the only person who had faced this problem, but no working solutions. So I have developed my own and present it for you here.

The first problem of course is to break the string up into words. Regex 5.5 will do this very nicely for us. What it will not do is to capitalise the words when it finds them. For this we need to process the found words with vbProperCase.

Here is the complete function for you

Public Function CapitaliseNames(ByVal strIn As String) As String
‘~~~~~
‘Name: CapitaliseNames
‘Version: 1.00
‘Purpose: capitalise the input name – handles hypens and apostrophes

‘Arguments:
‘ strIn = string to capitalise
‘Return:
‘ capitalised string

‘Author: David Soussan
‘Updated: 14/01/2010

‘Comments:
‘ requires reference to VBScript Regular Expression 5.5
‘~~~~~

On Error GoTo ErrorHandler
Dim strReturn As String
Dim oRegEx As RegExp
Dim Matches, Match
Const strPattern As String = “\b(\w+)\b”

‘~~~~~
‘use regex to find all words
‘~~~~~

10:
Set oRegEx = New RegExp
oRegEx.Pattern = strPattern
oRegEx.Global = True
Set Matches = oRegEx.Execute(strIn)

‘~~~~~
‘capitalise each word found
‘~~~~~

20:
For Each Match In Matches

strIn = Replace(strIn, Match.Value, StrConv(Match.Value, vbProperCase))

Next

‘~~~~~
‘lowercase common terms
‘~~~~~

30:
strIn = Replace(strIn, ” And “, ” and “)
strIn = Replace(strIn, ” Of “, ” of “)
strReturn = strIn

ExitProcedure:
CapitaliseNames = strReturn
Exit Function
ErrorHandler:
strReturn = “”
Select Case iLogError(Err.Description, CODE_MODNAME, “CapitaliseNames()”, Err.Number, Erl)
Case ERR_IGNORE
Resume Next
Case Else
Resume ExitProcedure
End Select
End Function

It does of course require my error handler – so substitute your own there (and sorry about the formatting – can’t seem to get copy/paste working in wordpress :( ) And do make sure that you keep the blank before and after the ‘ and ‘ n  ‘ or ‘ unless you want to spoil names like Anderson and Oregon :)

This entry was posted in Uncategorized and tagged , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

4 Comments

  1. Bill
    Posted May 15, 2010 at 7:36 pm | Permalink

    That is crazy unnecessary, dude, try this:

    Public Function Capitalize(ByVal strToCapitalize As String) As String

    ” add a reference to excel, if you are not in excel (since you don’t mind adding references)

    Capitalize = Excel.WorksheetFunction.Proper(strToCapitalize)
    Capitalize = Replace(Capitalize, ” And “, ” and “, 1, , vbTextCompare)
    Capitalize = Replace(Capitalize, ” Or “, ” or “, 1, , vbTextCompare)

    End Function

    Bill

  2. Posted June 20, 2010 at 1:46 pm | Permalink

    Well now, Bill, if I’d known about that Excel function then I would have used it. So thanks for the info.

    In Access VBA the equivalent using vbProperCase does not handle apostophies and hyphenated names. It was because I could not find anything like your solution out there that I came up with my own ‘unnecessary’ method.

  3. Posted May 14, 2011 at 6:22 am | Permalink

    I’m not easily irpmesesd. . . but that’s impressing me! :)

  4. Posted September 1, 2011 at 11:14 am | Permalink

    Awesome informations once again.! I am looking forward for your next post:)

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>