Run-time error '13': Type Mismatch

mr.mackey

New Member
Joined
Jul 28, 2011
Messages
19
Hi, looking for some help with this. I have the following code in my worksheet, which is supposed to pop-up a message when C19 has the value "BLUE"

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$C$19" And Target.Value = "BLUE" Then
MsgBox "Please ensure that you meant to select BLUE"

End If
End Sub

It works fine, however, it seems to be conflicting with a macro I have recorded that clears data in various cells. The macro code is as follows:


Sub ClearData()

Dim msgPrompt As String, msgTitle As String
Dim msgButtons As Integer, msgResult As Integer

msgPrompt = "Are you sure you want to clear all data?"
msgButtons = vbYesNo + vbQuestion + vbDefaultButton1
msgTitle = "Clear Data?"

msgResult = MsgBox(msgPrompt, msgButtons, msgTitle)

Select Case msgResult
Case vbYes

Sheets("Data Input").Select

With Application
.Calculation = xlManual
.MaxChange = 0.001
End With
ActiveWorkbook.PrecisionAsDisplayed = False

Range("C5:R8").Select
Selection.ClearContents

Range("C12:R34").Select
Selection.ClearContents

Range("C54:R55").Select
Selection.ClearContents

Range("C58:R58").Select
Selection.ClearContents

With Application
.Calculation = xlAutomatic
.MaxChange = 0.001
End With
ActiveWorkbook.PrecisionAsDisplayed = False

Case vbNo
Exit Sub
End Select
End Sub



The error I receive when I run this macro is "Run-time error '13': Type Mismatch".

Can someone help me solve this problem?

Thank you kindly!
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Welcome to MrExcel.

In your ClearData procedure put this at the beginning:

Application.EnableEvents = False

and:

Application.EnableEvents = True

at the end. That will prevent your Worksheet_Change event procedure from firing.
 
Upvote 0
Hello again, while that solution fixed my problem. I'm now getting the same run-time error when copying and pasting in that worksheet.

Can you provide a solution for that?

Thanks,
 
Upvote 0
On which line of code are you getting that error? Maybe try:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address = "$C$19" Then
        If Target.Value = "BLUE"
            MsgBox "Please ensure that you meant to select BLUE"
        End If
    End If
End Sub
 
Upvote 0
Change the event code to:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   If Not Intersect(Target, Range("C19")) Is Nothing Then
      If Range("C19").Value = "BLUE" Then
         MsgBox "Please ensure that you meant to select BLUE"
      End If
   End If
End Sub
 
Upvote 0
And how would I make this work for mutliple cells? Basically, I want the message box to appear if any cells from C19 to R19 are changed to BLUE.

Thanks
 
Upvote 0
Like so:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   Dim rngCell           As Excel.Range
   If Not Intersect(Target, Range("C19:R19")) Is Nothing Then
      For Each rngCell In Intersect(Target, Range("C19:R19"))
         If rngCell.Value = "BLUE" Then
            MsgBox "Please ensure that you meant to select BLUE"
         End If
      Next rngCell
   End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,518
Messages
6,179,253
Members
452,900
Latest member
LisaGo

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