We help IT Professionals succeed at work.

Force Cell contents to Uppercase

76 Views
Last Modified: 2018-10-25
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
Comment
Watch Question

AlanConsultant
CERTIFIED EXPERT

Commented:
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.
Martin LissSocial distance - Don't touch your face - Wash your hands for 20 seconds
CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
What version of Excel are you using? It doesn't happen with me using Excel 2010.
Fabrice LambertConsulting
CERTIFIED EXPERT
Distinguished Expert 2017

Commented:
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

Thomas ReidMaintenance Technician

Author

Commented:
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
AlanConsultant
CERTIFIED EXPERT

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

Alan.
Consulting
CERTIFIED EXPERT
Distinguished Expert 2017
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Thomas ReidMaintenance Technician

Author

Commented:
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!
Fabrice LambertConsulting
CERTIFIED EXPERT
Distinguished Expert 2017

Commented:
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.
Thomas ReidMaintenance Technician

Author

Commented:
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
Roy CoxGroup Finance Manager
CERTIFIED EXPERT

Commented:
Try this

Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)

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

Open in new window

Fabrice LambertConsulting
CERTIFIED EXPERT
Distinguished Expert 2017

Commented:
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.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.