[英]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. 当我运行
test()
子If Not Intersect(Target, Range("K:M")) Is Nothing
, If Not Intersect(Target, Range("K:M")) Is Nothing
收到类型不匹配错误,因为Target错误类型。
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!
如果手动过滤数据表的第14列以仅保留空白单元格,则不会出现相同的错误!
The problem with the type mismatch, is that the Target.Cells
is more than one cell. 类型不匹配的问题是
Target.Cells
超过一个单元。 Thus, the Target.Value <> ""
throws type mismatch, because multiple cells cannot be compared to ""
. 因此,
Target.Value <> ""
引发类型不匹配,因为不能将多个单元格与""
进行比较。 See the MsgbBox
with the number of cells: 请参阅
MsgbBox
以及单元格数:
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. 最简单的方法是编写
If Target.Cells.CountLarge > 1 Then Exit Sub
在_SelectionChange
事件中If Target.Cells.CountLarge > 1 Then Exit Sub
。
Another way is to disable events around 另一种方法是禁用周围的事件
sCount = wsData.Columns(14).SpecialCells(xlCellTypeBlanks).Count
like this: sCount = wsData.Columns(14).SpecialCells(xlCellTypeBlanks).Count
像这样:
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 我已经在导致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.
当我运行test()子程序时,如果目标不正确,则在If Not Intersect(Target,Range(“ K:M”))Nothing上收到类型不匹配错误。 Why this is happening?
为什么会这样呢?
When the procedure Test
triggers the Worksheet_SelectionChange
event, your code will fail on the line 当
Test
过程触发Worksheet_SelectionChange
事件时,您的代码将失败
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. 这是因为
Target.Value <> ""
是SpecialCells(xlCellTypeBlanks).Count
的罪魁祸首SpecialCells(xlCellTypeBlanks).Count
可能返回多个单元格。
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.