Run-time error '13': Type mismatch with Ucase

Aphten

New Member
Joined
Jul 24, 2024
Messages
11
Office Version
  1. 2021
Platform
  1. Windows
I am working on a worksheet_change where there are a number of select cells that I need to Ucase and a couple of cells that I need to ProperCase. Please see the code below. I put in each cell reference one by one, to see if there was an issue with one of the cells, because I just can't seem to get past this run-time error, and everything ran without issue. There are 2 cells that have a mix of numbers and letters and the rest have only letters.

Code:
    If Not Intersect(Target, Union(Range("AI13:AI14,H17,P17:P19,R16,T16:T19,AA17:AA18,AI16,G21,G24,S20:S22,AA20:AA22,AH21:AH25" _
    ), Range( _
    "AP21:AP24,AK28:AL28,AK30:AL30,AK32:AL32,AP28:AP31,AX32,AY44,AP45:AP47,AY48,AP49:AP54,AP56:AP60,AY64,AP65,AU65,C38:C65" _
    ), Range( _
    "V38:V65,W66,C67:C70,AJ67:AJ68,AV69,Y71:AW74,C72:C79,L80,R80,C81:C82,C86:C94,K86:K94,AD77,AC77:AD80,AK77:AL80,AS77:AT80" _
    ), Range( _
    "G96,G98,Y86,AG88,AG90:AG92,AG95,AG97:AG98,AO87:AU99" _
    ))) Is Nothing Then
'
        Application.ScreenUpdating = False
        Application.EnableEvents = False
'
            Target = UCase(Target)
'
        Application.ScreenUpdating = True
        Application.EnableEvents = True
'
End If
'
    If Not Intersect(Target, Range("J13,H37")) Is Nothing Then
'
        Application.ScreenUpdating = False
        Application.EnableEvents = False
'
            Target = StrConv(Target, vbProperCase)
'
        Application.ScreenUpdating = True
        Application.EnableEvents = True
'
End If

I added the following code. Please don't mind all the notes. I am not fluent in VBA so this saves me time when I have to go back and debug.

Code:
'A: MCD--> If the resident does not have MCD [AI14=NO], then subsequent cell is highlighted and the font color is changed to bold red.
'
Select Case Range("AI14").Value
Case "NO"
    Application.ScreenUpdating = False
    Application.EnableEvents = False
        Range("AI14").Interior.Color = RGB(255, 255, 0) 
        Range("AI14").Font.Color = RGB(255, 0, 0)      
        Range("AI14").Font.Bold = True                  
    Application.ScreenUpdating = True
    Application.EnableEvents = True
End Select
'
'A: MCD-->If the subsequent cell is blank[AI14=""], then the cell fill matches and font is normal.
'
Select Case Range("AI14").Value
Case ""
    Application.ScreenUpdating = False
    Application.EnableEvents = False
        Range("AI14").Interior.Color = RGB(226, 239, 218)
        Range("AI14").Font.Color = RGB(0, 112, 192)       
        Range("AI14").Font.Bold = False                   
    Application.ScreenUpdating = True
    Application.EnableEvents = True
End Select
'
'A: MCD-->If the subsequent cell has a MCD number, then the cell fill matches and font is normal.
'
Select Case Range("AI14").Value
Case Is <> "NO"
    Application.ScreenUpdating = False
    Application.EnableEvents = False
        Range("AI14").Interior.Color = RGB(226, 239, 218) 
        Range("AI14").Font.Color = RGB(0, 112, 192)      
        Range("AI14").Font.Bold = False                  
    Application.ScreenUpdating = True
    Application.EnableEvents = True
End Select

After I added the code above, I get run-time error 13 that says type mismatch. The following code is highlighted.
Code:
Target = UCase(Target)

What am I doing wrong? From what I read on the good old internet, it says I must be trying to convert something that isn't text, which I guess isn't compatible with the UCase function.

First I removed the 2 cells that have numbers and it still gives me the same error.
Then I removed the select case coding and I still get the same error.
Then I removed the ProperCase coding and I still get the same error.
I ended up putting everything (ProperCase and select case) back in EXCEPT the Ucase and I no longer get the run-time error. By process of elimination, there is something not running correctly with my Ucase coding or there is something in my worksheet that is not compatible with Ucase maybe? I do not have any conditional formatting or other macros or VBA coding that could be interfering.

Any suggestions?
 
One thing that would cause that error on that line of code is if more than one cell at a time is changed.
It is generally safer to process each cell individually in case that happens.

The code would go something like this

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim Changed As Range, c As Range
  
    Set Changed = Intersect(Target, Union(Range("AI13:AI14,H17,P17:P19,R16,T16:T19,AA17:AA18,AI16,G21,G24,S20:S22,AA20:AA22,AH21:AH25" _
    ), Range( _
    "AP21:AP24,AK28:AL28,AK30:AL30,AK32:AL32,AP28:AP31,AX32,AY44,AP45:AP47,AY48,AP49:AP54,AP56:AP60,AY64,AP65,AU65,C38:C65" _
    ), Range( _
    "V38:V65,W66,C67:C70,AJ67:AJ68,AV69,Y71:AW74,C72:C79,L80,R80,C81:C82,C86:C94,K86:K94,AD77,AC77:AD80,AK77:AL80,AS77:AT80" _
    ), Range( _
    "G96,G98,Y86,AG88,AG90:AG92,AG95,AG97:AG98,AO87:AU99" _
    )))
  
    If Not Changed Is Nothing Then
'
        Application.ScreenUpdating = False
        Application.EnableEvents = False
'
            For Each c In Changed
              c.Value = UCase(c.Value)
            Next c
'
        Application.ScreenUpdating = True
        Application.EnableEvents = True
'
    End If
'
    Set Changed = Intersect(Target, Range("J13,H37"))
    If Not Changed Is Nothing Then
'
        Application.ScreenUpdating = False
        Application.EnableEvents = False
'
            For Each c In Changed
              c.Value = StrConv(c.Value, vbProperCase)
            Next c
'
        Application.ScreenUpdating = True
        Application.EnableEvents = True
'
    End If
End Sub

If that does not solve the issue we might need details of how to set up a dummy worksheet, and what change(s) to make to it to test.
 
Upvote 0

Forum statistics

Threads
1,226,841
Messages
6,193,287
Members
453,788
Latest member
drcharle

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top