简体   繁体   中英

Why am I getting a type mismatch error here?

I create a new module and insert this code:

Sub test()
   Set wsData = ThisWorkbook.Worksheets("Data")
   sCount = wsData.Columns(14).SpecialCells(xlCellTypeBlanks).Count
   msgbox sCount
End Sub

In the worksheet "Data", I have this code:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Selection.CountLarge = 1 Then
        If Not Intersect(Target, Range("K:M")) Is Nothing And Target.Value <> "" Then
            'code
        End if
    End if
End Sub

When I run the test() sub, I get a type mismatch error on If Not Intersect(Target, Range("K:M")) Is Nothing , as Target wrong type.

Why this is happening?

Why is test triggering the Change Event? I dont get the same error if manually filter column 14 of my Data sheet to leave only the blank cells!

The problem with the type mismatch, is that the Target.Cells is more than one cell. Thus, the Target.Value <> "" throws type mismatch, because multiple cells cannot be compared to "" . See the MsgbBox with the number of cells:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Selection.CountLarge = 1 Then
        If Target.Cells.CountLarge > 1 Then MsgBox Target.Cells.CountLarge
        If Not Intersect(Target, Range("K:M")) Is Nothing And Target.Value <> "" Then
            'code
        End If
    End If
End Sub

Based on the business logic there could be several solutions.

  • The easiest one is to write If Target.Cells.CountLarge > 1 Then Exit Sub in the _SelectionChange event.

  • Another way is to disable events around

sCount = wsData.Columns(14).SpecialCells(xlCellTypeBlanks).Count like this:


Sub TestMe()
   Set wsData = ThisWorkbook.Worksheets("Data")
   Application.EnableEvents = False
   sCount = wsData.Columns(14).SpecialCells(xlCellTypeBlanks).Count
   Application.EnableEvents = True
   msgbox sCount
End Sub

I almost closed this question as a duplicate.

I will answer both your questions but in the reverse order so that you can understand it better.

Why is test triggering the Change Event?

I have explained it in SpecialCells causing SheetSelectionChange event in Excel 2010

When I run the test() sub, I get a type mismatch error on If Not Intersect(Target, Range("K:M")) Is Nothing, as Target wrong type. Why this is happening?

When the procedure Test triggers the Worksheet_SelectionChange event, your code will fail on the line

If Not Intersect(Target, Range("K:M")) Is Nothing And Target.Value <> "" Then

It is because Target.Value <> "" is the culprit as SpecialCells(xlCellTypeBlanks).Count may return multiple cells.

If you break the above line in 2 lines then you will not get an error

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Target.Cells.CountLarge > 1 Then Exit Sub
    If Not Intersect(Target, Range("K:M")) Is Nothing Then
        If Target.Value <> "" Then
             'code
        End If
    End If
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM