Avatar of Thomas Reid
Thomas ReidFlag for United Kingdom of Great Britain and Northern Ireland

asked on 

Force Cell contents to Uppercase

I have been using the following code to force any text entered into excel to Uppercase:

Private Sub Worksheet_Change(ByVal Target As Range)
Target.Value = VBA.UCase(Target.Value)
End Sub

Open in new window


This is working fine.
However, if I delete the contents of a cell I get a VBA
Run-time error '13':
Type mismatch

This error also appears if I paste into a cell.
Any ideas how I can get around that?
Thanks in advance
VBAMicrosoft ExcelMicrosoft Office

Avatar of undefined
Last Comment
Fabrice Lambert
Avatar of Alan
Alan
Flag of New Zealand image

The easiest option would be to insert an 'On Error Resume Next' in there, like this:

Private Sub Worksheet_Change(ByVal Target As Range)
On Error Resume Next
Target.Value = VBA.UCase(Target.Value)
On Error Goto 0
End Sub

Open in new window

This does have the downside of also masking other errors, but in something so short and simple, that should not be a concern to obsess over.

You don't really *need* the 'On Error Goto 0' at the end, since the full code finishes at that point, but if using in a more elaborate code section, then you would normally want to return error handling to 'standard' as soon as possible.

Alan.
Avatar of Martin Liss
Martin Liss
Flag of United States of America image

What version of Excel are you using? It doesn't happen with me using Excel 2010.
Avatar of Fabrice Lambert
Fabrice Lambert
Flag of France image

On Error Resume next is not only the "easy" way, it is also the "lazy" way. I do not recommend it, as it teach bad practices (ignoring errors)., and believe me, bad practices are tough to loose when you become used to use them.

If you delete the content of the cell, its value becomes null or empty. So no surprise the UCase function fail as its argument must be a string (and strings do not support null value).

Concatenate the cell's value with an blank string, if the result is not a blank string, call UCase, else do nothing.
Private Sub Worksheet_Change(ByVal Target As Range)
    If((Target.Value & vbNullString) <> vbNullString) Then
        Target.Value = UCase(Target.Value)
    End If
End Sub

Open in new window

Avatar of Thomas Reid
Thomas Reid
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

Thanks for the response guys.

Alan your code seems to work fine, however it seems to make the workbook run slow?

Martin I'm using Excel 2013

Fabrice I am still getting an the same error upon cell deletion or paste using your code.

Cheers
Avatar of Alan
Alan
Flag of New Zealand image

Try without the last 'On Error Goto 0', but I don't see why that would make any significant difference to speed.

Alan.
ASKER CERTIFIED SOLUTION
Avatar of Fabrice Lambert
Fabrice Lambert
Flag of France image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Thomas Reid
Thomas Reid
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

Thanks for the help Alan, but it was still making the workbook run slow.

Your last suggest code seemed to do the trick Fabrice!

Many thanks all!
Avatar of Fabrice Lambert
Fabrice Lambert
Flag of France image

This might be even faster as Excel is able to convert ranges to array (and vice versa), and VBA compute arrays faster than ranges:
Private Sub Worksheet_Change(ByVal Target As Range)
    If (Target.Cells.Count > 1) Then
            '// more than one cell
        Dim data() As Variant
        data = Target.value
        
        Dim i As Long, j As Long
        For i = LBound(data, 1) To UBound(data, 1)
            For j = LBound(data, 2) To UBound(data, 2)
                If ((data(i, j) & vbNullString) <> vbNullString) Then
                    data(i, j) = UCase(data(i, j))
                End If
            Next
        Next
        Target.value = data
    Else
             '// only one cell
        If ((Target.value & vbNullString) <> vbNullString) Then
            Target.value = UCase(Target.value)
        End If
    End If
End Sub

Open in new window

Side note: This might mess up with formulas.
Avatar of Thomas Reid
Thomas Reid
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

That code made the workbook even slower.

Would it help to know that only 1 cell will be getting edited at a time?

It seems as though when using any of the codes, after inputting text into the cell excel then lags as it calculates something in the background.

FYI, there are no formula in this workbook, just a form for recording some tasks
Avatar of Roy Cox
Roy Cox
Flag of United Kingdom of Great Britain and Northern Ireland image

Try this

Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)

Target.Value = StrConv(Target.Text, vbProperCase)
End Sub

Open in new window

Avatar of Fabrice Lambert
Fabrice Lambert
Flag of France image

Would it help to know that only 1 cell will be getting edited at a time?
In the case of a single cell, the value property retrieve a single value, wich can be implicitly converted to string.
But in the case of multiple cells, the value property retrieve an 2D array, wich isn't convertible to string.

That code made the workbook even slower
How many cells are you moving / cut-pasting / copy-pasting / deleting ?
The whole worksheet ?
A bunch of rows ?
A bunch of columns ?
Ouch ! Our solutions arn't tailored to work with that many cells.
Microsoft Excel
Microsoft Excel

Microsoft Excel topics include formulas, formatting, VBA macros and user-defined functions, and everything else related to the spreadsheet user interface, including error messages.

144K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo