VBA RunTime Error 13: Type Mismatch Worksheet Change Event

truebluewoman

New Member
Joined
Sep 26, 2014
Messages
36
I have a range of cells that if the value is changed to "R", then it should clear the contents of the certain cells to their right. The code actually works, but then I get an error. "RunTime Error '13' Type Mismatch.

When I debug, it appears that the "offending" piece of the code is the third line which is the condition of whether or not the contents to the right should be cleared out. I read on a separate thread that "Target" is a range of cells, so using "Target" in my conditions is wrong. How do I write the condition so I do not get this error? Can anyone help me fix this code? Thank you!


VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

If Intersect(Target, Range("ALL_BED_STATUS")) Is Nothing Then

    If Target = "R" Then
    
        ActiveSheet.Range(Target.Offset(, 4), Target.Offset(, 12)).ClearContents

    End If

End If

On Error GoTo ErrorHandler
ErrorHandler:

Exit Sub

End Sub
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
You are going redundant when you use ClearContents since your macro starts all over again. A ClearContents is considered a change in your sheet so event Worksheet_Change triggers. Use:
VBA Code:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
    If Intersect(Target, Range("ALL_BED_STATUS")) Is Nothing Then
        If Target = "R" Then
            Application.EnableEvents = False      '<-added
            ActiveSheet.Range(Target.Offset(, 4), Target.Offset(, 12)).ClearContents
            Application.EnableEvents = True       '<-added
        End If
    End If
    On Error GoTo ErrorHandler
ErrorHandler:
    Exit Sub
End Sub
 
Last edited:
Upvote 0
Solution
You are going redundant when you ClearContents so your macro starts all over again, use:
VBA Code:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
    If Intersect(Target, Range("ALL_BED_STATUS")) Is Nothing Then
        If Target = "R" Then
            Application.EnableEvents = False      '<-added
            ActiveSheet.Range(Target.Offset(, 4), Target.Offset(, 12)).ClearContents
            Application.EnableEvents = True       '<-added
        End If
    End If
    On Error GoTo ErrorHandler
ErrorHandler:
    Exit Sub
End Sub
That worked perfectly! I tried the Application.EnableEvents earlier, but I put it in the wrong area of the code and it just turned off events and did nothing:( Thank you SO much!
 
Upvote 0
Also the way you're using the error handler seems wrong to me. See how this goes:

VBA Code:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)

    On Error GoTo ErrorHandler

    If Intersect(Target, Range("ALL_BED_STATUS")) Is Nothing Then
        If StrConv(Target, vbUpperCase) = "R" Then
            Application.EnableEvents = False
                Range(Target.Offset(, 4), Target.Offset(, 12)).ClearContents
            Application.EnableEvents = True
        End If
    End If

Exit Sub

ErrorHandler:

    Application.EnableEvents = True 'Ensure macros are enabled in the event of an error with the above code

End Sub

Regards,

Robert
 
Upvote 0

Forum statistics

Threads
1,223,970
Messages
6,175,702
Members
452,667
Latest member
vanessavalentino83

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