Excel 工作表\u更改,相交不匹配错误
如果能帮上忙,我将不胜感激。删除整行内容或多行内容时,我在第行收到一个不匹配错误:Excel 工作表\u更改,相交不匹配错误,excel,vba,Excel,Vba,如果能帮上忙,我将不胜感激。删除整行内容或多行内容时,我在第行收到一个不匹配错误:If.Value=“confirm”Then Private Sub Worksheet_Change(ByVal Target As Range) Dim ws As Worksheet Set ws = ThisWorkbook.Sheets("Expedia") If Not Intersect(Target, Range("I2:I1000")) Is Nothing Then
If.Value=“confirm”Then
Private Sub Worksheet_Change(ByVal Target As Range)
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("Expedia")
If Not Intersect(Target, Range("I2:I1000")) Is Nothing Then
With Target
If .Value = "CONFIRMED" Then
.Offset(0, 8).Value = "NEW"
.Offset(0, 9).Value = Date
ElseIf .Value = "" Then
.Offset(0, 10).Value = "CHANGED"
.Offset(0, 11).Value = Date
ElseIf .Value = "INVALID" Then
.Offset(0, 12).Value = "INVALID"
.Offset(0, 13).Value = Date
End If
End With
End If
当
Target
是多个单元格时,它会发出抱怨
您可以通过检查子项开始处的目标计数来保护该案例:
If Target.Count <> 1 Then Exit Sub
如果目标计数为1,则退出Sub
当目标
不止一个单元格时,它会抱怨
您可以通过检查子项开始处的目标计数来保护该案例:
If Target.Count <> 1 Then Exit Sub
如果目标计数为1,则退出Sub
请始终记住,目标也可以是多个单元格的范围。例如,如果复制/粘贴某些单元格。因此,如果要覆盖粘贴的所有单元格,则需要遍历与所需范围相交的所有受影响单元格I2:I1000
Private Sub Worksheet_Change(ByVal Target As Range)
Dim AffectedCells As Range 'get all cells that were canged and intersect with I2:I1000
Set AffectedCells = Intersect(Target, Me.Range("I2:I1000"))
If Not AffectedCells Is Nothing Then
Application.EnableEvents = False 'disable events to prevent re-triggering this event when writing to cells
On Error Goto ERR_HANDLING
Dim Cell As Range
For Each Cell In AffectedCells 'loop through all of the affected cells in I2:I1000
With Cell
If .Value = "CONFIRMED" Then
.Offset(0, 8).Value = "NEW"
.Offset(0, 9).Value = Date
ElseIf .Value = "" Then
.Offset(0, 10).Value = "CHANGED"
.Offset(0, 11).Value = Date
ElseIf .Value = "INVALID" Then
.Offset(0, 12).Value = "INVALID"
.Offset(0, 13).Value = Date
End If
End With
Next Cell
End If
ERR_HANDLING: 'in the end or in case of error enable events or they stay disabled!
Application.EnableEvents = True
If Err.Number <> 0 Then
Err.Raise Err.Number, Err.Source, Err.Description, Err.HelpFile, Err.HelpContext
End If
End Sub
因为从未使用过变量ws
。如果要参考事件(和Target
)所在的工作表,请使用Me
类似Me.Range(“I2:I1000”)
请始终记住,目标
也可以是一系列多个单元格。例如,如果复制/粘贴某些单元格。因此,如果要覆盖粘贴的所有单元格,则需要遍历与所需范围相交的所有受影响单元格I2:I1000
Private Sub Worksheet_Change(ByVal Target As Range)
Dim AffectedCells As Range 'get all cells that were canged and intersect with I2:I1000
Set AffectedCells = Intersect(Target, Me.Range("I2:I1000"))
If Not AffectedCells Is Nothing Then
Application.EnableEvents = False 'disable events to prevent re-triggering this event when writing to cells
On Error Goto ERR_HANDLING
Dim Cell As Range
For Each Cell In AffectedCells 'loop through all of the affected cells in I2:I1000
With Cell
If .Value = "CONFIRMED" Then
.Offset(0, 8).Value = "NEW"
.Offset(0, 9).Value = Date
ElseIf .Value = "" Then
.Offset(0, 10).Value = "CHANGED"
.Offset(0, 11).Value = Date
ElseIf .Value = "INVALID" Then
.Offset(0, 12).Value = "INVALID"
.Offset(0, 13).Value = Date
End If
End With
Next Cell
End If
ERR_HANDLING: 'in the end or in case of error enable events or they stay disabled!
Application.EnableEvents = True
If Err.Number <> 0 Then
Err.Raise Err.Number, Err.Source, Err.Description, Err.HelpFile, Err.HelpContext
End If
End Sub
因为从未使用过变量ws
。如果要参考事件(和Target
)所在的工作表,请使用Me
类似Me.Range(“I2:I1000”)
这很奇怪,因为要求整行的.Value
没有意义<代码>目标
是一个范围
对象,因此它可以包含一个或多个单元格。也许,如果您只想为单个单元格触发此命令,您可以添加类似于的内容,如果Target.Cells.Count=1,那么
或者按照@jnevil的建议,将其限制为仅一个单元格,您可以循环通过所有受影响的单元格(相交)。请参阅下面的答案。这很奇怪,因为要求整行的.Value
没有意义<代码>目标
是一个范围
对象,因此它可以包含一个或多个单元格。也许,如果您只想为单个单元格触发此命令,您可以添加类似于的内容,如果Target.Cells.Count=1,那么
或者按照@jnevil的建议,将其限制为仅一个单元格,您可以循环通过所有受影响的单元格(相交)。请参阅下面的答案。您还应该检查范围内的多个区域:,用于AffectedCells中的每个区域。区域。。。。对于.Cells
@ExcelDevelopers区域中的每个单元格,虽然在某些情况下可能需要这样做,但在这种情况下实际上不需要这样做。测试一下。请参见我添加的动画(它粘贴在不同的区域)但是我添加了一个EnableEvents=False
,这是一个好主意。您还应该检查范围内的多个区域:用于受影响单元格中的每个区域。区域。。。。对于.Cells
@ExcelDevelopers区域中的每个单元格,虽然在某些情况下可能需要这样做,但在这种情况下实际上不需要这样做。测试一下。请参见我添加的动画(它粘贴在不同的区域)但是我添加了一个EnableEvents=False
,这是个好主意。