VBA Worksheet_Change - Type mismatch runtime error

btc3111

New Member
Joined
Feb 1, 2022
Messages
6
Office Version
  1. 2021
  2. 2016
Platform
  1. Windows
Hello all! @rollis13 was kind enough to combine 3 VBA Worksheet_Change's for me. In particular, it had to do incorporating only certain characters in certain ranges and also make said ranges only possible to be Capital letters. Everything works but when selecting multiple ranges, I receive a Microsoft Visual Basic popup stating "Run-time error '13': Type mismatch". With the Debug function highlighting this portion of the code .Value = UCase(.Value).
If anyone can fix this, it would be greatly appreciated!


VBA Code:
Option Explicit
Private Const FCheckRgAddress As String = "M12:CI36"
Private Const FCheckRgAddress_2 As String = "L45:T60,W45:AE60,AH45:AP60"
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim xChanged As Range
    Dim xRg    As Range
    Dim xString As String
    Dim sErrors As String
    Dim xRegExp As Variant
    Dim xHasErr As Boolean
    Set xRegExp = CreateObject("VBScript.RegExp")
    xRegExp.Global = True
    xRegExp.IgnoreCase = True
    Set xChanged = Application.Intersect(Range(FCheckRgAddress), Target)
    If xChanged Is Nothing Then GoTo second
    xRegExp.Pattern = "[^A,B,C,F,I,N,O,P,S,T,X]"
    For Each xRg In xChanged
        If xRegExp.TEST(xRg.Value) Then
            xHasErr = True
            Application.EnableEvents = False
            xRg.ClearContents
            Application.EnableEvents = True
        End If
    Next
    If xHasErr Then MsgBox "These cells had invalid entries and have been cleared:"
second:
    Set xChanged = Application.Intersect(Range(FCheckRgAddress_2), Target)
    If xChanged Is Nothing Then GoTo third
    xRegExp.Pattern = "[^A,B,C,I,N,O,T,X]"
    For Each xRg In xChanged
        If xRegExp.TEST(xRg.Value) Then
            xHasErr = True
            Application.EnableEvents = False
            xRg.ClearContents
            Application.EnableEvents = True
        End If
    Next
    If xHasErr Then MsgBox "These cells had invalid entries and have been cleared:"
third:
    If Not (Application.Intersect(Target, Range("M12:CI36,L45:T60,W45:AE60,AH45:AP60")) Is Nothing) Then
        With Target
            If Not .HasFormula Then
                Application.EnableEvents = False
                .Value = UCase(.Value)
                Application.EnableEvents = True
            End If
        End With
    End If
End Sub
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
When exactly is in the cell that is trying to be updated?
Do you have any errors in the ranges you are checking?
 
Upvote 0
Hello all! @rollis13 was kind enough to combine 3 VBA Worksheet_Change's for me. In particular, it had to do incorporating only certain characters in certain ranges and also make said ranges only possible to be Capital letters. Everything works but when selecting multiple ranges, I receive a Microsoft Visual Basic popup stating "Run-time error '13': Type mismatch". With the Debug function highlighting this portion of the code .Value = UCase(.Value).
If anyone can fix this, it would be greatly appreciated!
Sorry, I don't know how to edit my post. To elaborate, my error occurs when selecting multiple ranges and then deleting said range.
 
Upvote 0
When exactly is in the cell that is trying to be updated?
Do you have any errors in the ranges you are checking?
Sorry, should've elaborated, i'm having issues with any of the allowed characters from the code left in the cells when multi deleting them
 
Upvote 0
If you are deleting ranges in that scenario, then you DON'T want the code to run in those scenarios, right (there would be no range to run it on)!

People often prevent this sort of error from happening by adding a line like this to the top of their "Worksheet_Change" code:
VBA Code:
If Target.CountLarge > 1 Then Exit Sub

What this does it limit the code to only running when a SINGLE cell is changed at a time.
(When you delete whole rows, whole columns, or multiple cells, multiple cells are updated at once).

If you have need for the code to run on multiple cells at once (i.e. if you were to copy one cell over to ten, and wanted the code to run on all ten), then we would need to get more creative in the code.
 
Upvote 0
If you are deleting ranges in that scenario, then you DON'T want the code to run in those scenarios, right (there would be no range to run it on)!

People often prevent this sort of error from happening by adding a line like this to the top of their "Worksheet_Change" code:
VBA Code:
If Target.CountLarge > 1 Then Exit Sub

What this does it limit the code to only running when a SINGLE cell is changed at a time.
(When you delete whole rows, whole columns, or multiple cells, multiple cells are updated at once).

If you have need for the code to run on multiple cells at once (i.e. if you were to copy one cell over to ten, and wanted the code to run on all ten), then we would need to get more creative in the code.

You're on the money. I do need to be able to copy multiple cells. Its meant to be a used as a editable layout tool that will calculate only particular characters. Where can i go from here?
 

Attachments

  • CONCEPT.PNG
    CONCEPT.PNG
    36.1 KB · Views: 7
Upvote 0
There are a few options I can think of off the top of my head.

1. Disable the ability to delete ranges on the sheet altogether, and then write your own macro for deleting range (then you can tell it to disable events before running, so it will not trigger the "Worksheet_Change" event procedure code in that case).

2. Try to incorporate something like the "marker" logic mentioned in this thread here: Determine whether user is adding or deleting rows
 
Upvote 0
Since you will be entering letters one at a time all you have to do to avoid the issue when deleting multi-cells is to do what @Joe4 suggested and explained in his post #5.
 
Upvote 0

Forum statistics

Threads
1,214,384
Messages
6,119,201
Members
448,874
Latest member
Lancelots

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