我的代码大部分工作,但是当我清除新条目的数据范围时,我得到运行时错误13并且必须在我重新启动数据条目之前结束当前的宏。最重要的是,之前突出显示的单元格仍然突出显示。知道如何防止这种情况吗? enter image description here
我的代码是:
Private Sub Worksheet_Change(ByVal Target As Range)
Set lowspec = Range("i6")
Set highspec = Range("m6")
Set i = Intersect(Target, Range("f16:l34"))
If Not i Is Nothing Then
Select Case Target
Case 1 To lowspec: newcolor = 3
Case highspec + 1 To 1000: newcolor = 3
End Select
Target.Interior.ColorIndex = newcolor
End If
End Sub
答案 0 :(得分:1)
当您在一次操作中更改多个单元格时,Target
代表整个范围,因此Select Case Target
将成为一个问题。
以下是您需要为此编写代码的方法:
Target
可能超过一个小区Target
进行操作,那么可能仍然包括您感兴趣范围之外的细胞。类似的东西:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim lowSpec As Double, highSpec As Double, newcolor
Dim i As Range, c As Range, v
lowSpec = Me.Range("i6")
highSpec = Me.Range("m6")
Set i = Intersect(Target, Me.Range("f16:l34"))
If Not i Is Nothing Then
For Each c In i.Cells
v = c.Value
If Len(v) > 0 Then
Select Case v
Case 1 To lowSpec: newcolor = 3
Case highSpec + 1 To 1000: newcolor = 3
End Select
Else
newcolor = xlNone
End If
c.Interior.ColorIndex = newcolor
Next c
End If
End Sub