Proper, and other Text changes

Location:   http://www.mvps.org/dmcritchie/excel/proper.htm
Home page: http://www.mvps.org/dmcritchie/excel/excel.htm
[View without Frames]

If you want the contents of formulas to also be converted use Bill Manville's code or a variation. (see related below)


This page contains some VBA macros.  If you need assistance to install or to use a macro please refer to my  GetFormula  page.

Coding for this page, is available in another directory with names corresponding to some of the HTML files in the current directory.

keywords:  capitalization, lettercase, lower case, lowercase, lower-case, proper case, title case, upper case, upper-case, uppercase

PROPER

You can change your all uppercase to PROPER case with a worksheet function.  This can help with a list of names and addresses that was in all uppercase (capitals) or lowercase.  This is only to help fix the problems it will not properly fix names like "McRitchie" or "van der Beck".    (Well it fixes my name now -- just as an example, of course).

HELP --> Index --> PROPER Worksheet Function

=PROPER(a1)

Using a worksheet function you would have to copy then paste special and delete the original afterwards.

A macro works a lot easier.  Simply select a range of cells and invoke the macro.  Include additional code in macro to speed up results.

Sub Proper_Case()
     Application.ScreenUpdating = False
     Application.Calculation = xlCalculationManual
     Dim Cell as Range
     On Error Resume Next   'In case no cells in selection
     For Each Cell In Intersect(Selection, _
            Selection.SpecialCells(xlConstants, xlTextValues))
        Cell.Value = Application.Proper(Cell)
        '--- this is where you would code generalized changes for lastname
        '--- applied to names beginning in position 1 of cell
        If Left(Cell.Value, 2) = "Mc" Then Cell.Value = _
           "Mc" & UCase(Mid(Cell.Value, 3, 1)) & Mid(Cell.Value, 4, 99)
        If Left(Cell.Value, 3) = "Mac" Then Cell.Value = _
           "Mac" & UCase(Mid(Cell.Value, 4, 1)) & Mid(Cell.Value, 5, 99)
     Next
     '-- some specific text changes to lowercase, not in 1st position
     Selection.Replace What:=" a ", Replacement:=" a ", _
       LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False
     Selection.Replace What:=" and ", Replacement:=" and ", _
       LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False
     Selection.Replace What:=" at ", Replacement:=" at ", _
       LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False
     Selection.Replace What:=" for ", Replacement:=" for ", _
       LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False
     Selection.Replace What:=" from ", Replacement:=" from ", _
       LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False
     Selection.Replace What:=" in ", Replacement:=" in ", _
       LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False
     Selection.Replace What:=" of ", Replacement:=" of ", _
       LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False
     Selection.Replace What:=" on ", Replacement:=" on ", _
       LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False
     Selection.Replace What:=" the ", Replacement:=" the ", _
       LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False
     '--- This is where you would code specific name changes
     '--- regardless of position of character string in the cell
     Selection.Replace What:="mcritchie", Replacement:="McRitchie", _
       LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False
     Selection.Replace What:="delouis", Replacement:="DeLouis", _
       LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False
     Application.Calculation = xlCalculationAutomatic
     Application.ScreenUpdating = True
  End Sub

Some notations on the above code

Example of Current Region involved when a single cell is selected

As previously described here is an example where a single cell is selected but the use of "Selection.SpecialCells(xlCellTypeConstants)" would cause cell involvement from the current region instead of a maximum of 1 cell.
  Sub DemoSingleCellSelect()
    Dim cell As Range
    Range("a1:k10") = "aa"   '-- try later with  = "=1+1"
    Range("a3:f4").Clear
    Range("a1").Select
    on error resume next    '-- in case no cells selected
    For Each cell In Selection.SpecialCells(xlCellTypeConstants)
       cell.Interior.colorindex = 38
   Next cell
  End Sub

Things to watch out for when using macros, your own or someone else's

The following could be substituted in these macros: Cell.Value = StrConv(Cell,vbProperCase)

Here is a solution from Tom Ogilvy 1999-02-07) that changes cells that are constants on the ENTIRE spreadsheet.  The macro provided at the top will do the same if you Select ALL cells first before invoking the macro, but you might want to compare what is the same and what differs.  One big difference will be the speed because it has not turned off calculation and screen updating.

   Sub MakeProper()
     Dim Cell as Range, rng1 as Range
     Set rng1 = ActiveSheet.UsedRange.SpecialCells(xlConstants, xlTextValues)
     For Each cell In rng1
        cell.Value = Application.Proper(cell.Value)
     Next cell
   End Sub
For XL97 and above use xlCellTypeConstants instead of xlConstants

Lowercase

The worksheet function LOWER works the same as the PROPER worksheet function.

Example of a macro: (turn off screen updating and calculation in macro for quicker results)

Sub Lower_Case()
   Application.ScreenUpdating = False
   Application.Calculation = xlCalculationManual          'in XL97
   Dim Cell as Range
   On Error Resume Next   'In case no cells in selection
   For Each Cell In Intersect(Selection, _
          Selection.SpecialCells(xlConstants, xlTextValues))
      Cell.Value = LCase(Cell.Value)
   Next
   Application.Calculation = xlCalculationAutomatic     'in XL97
   Application.ScreenUpdating = True
End Sub

Uppercase

The worksheet function UPPER works the same as the PROPER worksheet function.  I would have to say that except for zip state codes I have not much use for converting everything to uppercase.

Example of a macro: (turn off screen updating and calculation in macro for quicker results)

Sub Upper_Case()
   Application.ScreenUpdating = False
   Application.Calculation = xlCalculationManual
   Dim Cell as Range
   On Error Resume Next   'In case no cells in selection
   For Each Cell In Intersect(Selection, _
          Selection.SpecialCells(xlConstants, xlTextValues))
      Cell.Value = UCase(Cell.Value)
   Next
   Application.Calculation = xlCalculationAutomatic
   Application.ScreenUpdating = True
End Sub
If appearance is all that matters there are some uppercase only fonts like Bank Gothic Mb BT, CopprplGoth [Hv] BT (Cooperpot), Desdemona, Bassoon.  You can find what you actually have by bringing the Unicode Character Map (shortcut) typing in your name in mixed case, and using the ArrowDN key to cycle through the fonts.  Excel and other windows software does not provide access to codepages which could have been built to have upppercase only.  Subset under font is as close as you will get to codepages.

The following would change entries immediately as they are created using an Event macro.  An Event Macro is apparently not effective until the workbook containing the macro is saved.  Note event macros are stored with the worksheet unlike normal macros which are stored in modules.

Option Explicit
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
'to install -- rightclick on the sheettab of the corresponding
' sheet and choose 'view code'.  Paste the following procedure
' in the module.  -- this is for EVENT macros ONLY.
  If Target.Column <> 1 Then Exit Sub
  If Target.Row = 1 Then Exit Sub
  Application.EnableEvents = False
  Target.Formula = UCase(Target.Formula)
  Application.EnableEvents = True
End Sub

TrimSUB - trim cells in selection

The TRIM Worksheet function removes spaces from beginning and end of text cells as well as extra internal spaces.  Removes all spaces from text except for single spaces between words.  The VBA equivalent only trims leading and trailing spaces.  (turn off screen updating and calculation in macro for quicker results)
Sub TrimSUB()
   Dim icell as Range
   For Each icell In Intersect(Selection, _
         Selection.SpecialCells(xlConstants, xlTextValues))
      icell.Value = Trim(icell.Value)
   Next icell
End Sub

Insert Blank Rows

For this example 2001-10-02, the poster requested that 2 blank rows be inserted for when the next cell in selected column is greater than the cell in the previous row of the column.  Note use of INTERSECT.
Sub InsertBlankRows()
    Dim i As Long   'integer not sufficient
    Dim nRange As Range
    Set nRange = Intersect(Selection, ActiveSheet.UsedRange, _
       ActiveCell.EntireColumn)
    If nRange.Cells.Count < 2 Then Exit Sub
    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual
    For i = nRange.Cells.Count To 2 Step -1
       If nRange.Item(i) > nRange.Item(i - 1) And _
            Not IsEmpty(nRange.Item(i - 1)) Then
            nRange.Item(i).Resize(2, 1).EntireRow.Insert
       End If
    Next
    Application.Calculation = xlCalculationAutomatic
    Application.ScreenUpdating = True
End Sub
The example was included here because of the use of INTERSECT and the manner of the loop, more comprehensive information on Inserting Rows.

Select cells matching activecell in the Selection area

Make new selections based on an original single (rectangular) selection based on the position of the active cell. In Excel 2000 you can use multiple selections with the Ctrl key which means the last selection, which can be a single cell, would be your active cell without any extra effort. (posted programming 2001-04-04).

With an initial selections of K8:Q15,K9:K9, would have K9 as the activecell

The resulting range of the example at right would be
Range("K9:K9,K10:K10,K11:K11,L11:L11,M11:M11,O11:O11," _
  &  "P11:P11,Q11:Q11,Q12:Q12,Q13:Q13,Q14:Q14,Q15:Q15").Select

Sub S_active()
  Dim cell As Range
  Dim i As Long
  Dim sStr As String
  For Each cell In Intersect(Selection, _
      Selection.SpecialCells(xlConstants, xlTextValues))
    i = i + 1
    If cell.Value = ActiveCell.Value Then _
       sStr = sStr & "," & cell.Address(0, 0) _
       & ":" & cell.Address(0, 0)
  Next cell
  Range(Mid(sStr, 2, Len(sStr) - 1)).Select
End Sub
K8L8M8N8O8P8 Q8
K9L9 M9N9O9P9Q9
K9L10 M10N10O10P10Q10
K9 K9 K9N11 K9 K9 K9
K12L12M12N12O12P12 K9
K13L13M13N13O13P13 K9
K14L14M14N14O14P14 K9
K15L15M15N15O15P15 K9

Checking for Numeric Cells in a Worksheet Selection

The following may look a bit out of place on this page, but is included here because of use of celltypes ie. xlCellTypeFormulas, xlCellTypeConstants

You can make similar macros named chkText and chkAllText to check for text instead by using xlTextValues instead of the 1 used in place of xlNumbers.

Sub chkAllNumeric()
    Cells.Select
    chkNumeric
End Sub
Sub chkNumeric()
    'David McRitchie in misc  2001-04-14  will be in proper.htm
    '   http://www.mvps.org/dmcritchie/excel/proper.htm
    'Something similar can be done with C.F.  =ISNUMBER(A1)
    Dim fNumeric As Range, cNumeric As Range
    On Error Resume Next
    Set fNumeric = Selection.SpecialCells(xlCellTypeFormulas, 1)
    Set cNumeric = Selection.SpecialCells(xlCellTypeConstants, 1)
    If fNumeric Is Nothing Then
       If cNumeric Is Nothing Then
          MsgBox "No numeric cells in selection"
       Else
          cNumeric.Select
       End If
    Else
       If cNumeric Is Nothing Then
          fNumeric.Select
       Else
          Union(fNumeric, cNumeric).Select
       End If
    End If
End Sub
An unreliable visual indicator would be to look at the cell: if left justified text, if right justified numberic. This can be overridden by cell justification, so select the cell and make sure it is not specifically overridden with left/center/right justified.

To check a lot of cells would be to select all cells (Ctrl+A) then Edit menu, GoTo, [Special],
    Constants & Number
    Formulas & Number
Unfortunately you can't do both at same time. Also note this is where you would choose "Blanks" to see if a cell is really Empty (no content, no formulas).

Use a Worksheet Function on an individual cell
    =ISTEXT(A1)
    =ISNUMBER(A1)

Use a Conditional Formatting with =ISNUMBER(A1) with one color and =ISTEXT(A1) with another color as described by Rob Bovey. Conditional Formatting will do no damage to your Excel File. To remove C.F. you will have to find C.F. usage which you can do by selecting a cell identified in the range and then Format --> C.F. as was used in creation.

AutoCorrect

There are some things that cause people problems with Auto Correct (Tools menu, Autocorrect)

Related Items

How to install/use a macro can be found on my formula

HELP --> StrConv     [vbUpperCase, vbLowerCase, vbProperCase]
= StrConv(Cell,vbLowerCase) page.

Related Information on My Excel Pages:

Related Information in Google Usenet Archives

Microsoft Knowledge Data Base (MS KB)

Q107564 XL: Not All Worksheet Functions Supported as Application (in VBA)
Run-Time Error '438':

Object doesn't support this property or method

You are one of many distinguished visitors who have visited my site here or in a previous location  since July 17, 1998. 

Visit [my Excel home page]   [Index page]   [Excel Onsite Search]   [top of this page]

Please send your comments concerning this web page to:  David McRitchie  mailto:DMcRitchie@msn.com