Why am I getting a type mismatch error here?
我创建一个新模块并插入以下代码:
1 2 3 4 5 | Sub test() Set wsData = ThisWorkbook.Worksheets("Data") sCount = wsData.Columns(14).SpecialCells(xlCellTypeBlanks).Count msgbox sCount End Sub |
在工作表" Data "中,我有以下代码:
1 2 3 4 5 6 7 | 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 |
运行
为什么会这样?
为什么测试会触发更改事件?
如果手动过滤数据表的第14列以仅保留空白单元格,我不会出现相同的错误!
类型不匹配的问题是
1 2 3 4 5 6 7 8 | 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 |
根据业务逻辑,可能有几种解决方案。
-
最容易的是写
_SelectionChange 事件中的If Target.Cells.CountLarge > 1 Then Exit Sub 。 -
另一种方法是禁用
附近的事件
1 2 3 4 5 6 7 | Sub TestMe() Set wsData = ThisWorkbook.Worksheets("Data") Application.EnableEvents = False sCount = wsData.Columns(14).SpecialCells(xlCellTypeBlanks).Count Application.EnableEvents = True msgbox sCount End Sub |
我几乎以重复的形式结束了这个问题。
我将按照相反的顺序回答您的两个问题,以便您更好地理解它。
Why is test triggering the Change Event?
我已经在导致Excel 2010中的SheetSelectionChange事件的SpecialCells中对此进行了解释
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?
当过程
1 | If Not Intersect(Target, Range("K:M")) Is Nothing And Target.Value <>"" Then |
这是因为
如果将上述行分成两行,则不会出现错误
1 2 3 4 5 6 7 8 | 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 |