|
代码如下:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Target.Row = 1 Then Exit Sub
'判断target是否与A1单元格底色一致,一致才相应计数
If Target.Interior.ColorIndex <> [a1].Interior.ColorIndex Then Exit Sub
Cancel = True
If IsNumeric(Cells(Target.Row, Target.Column + 1)) = True Then
Cells(Target.Row, Target.Column + 1) = Cells(Target.Row, Target.Column + 1) + IIf(Cells(1, 1).Value = "双击计数,右键减少", 1, -1)
Else
If MsgBox("所选单元格计数框非数字,是否清空?", vbOKCancel) = 1 Then
Cells(Target.Row, Target.Column + 1) = -1
End If
End If
End Sub
Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)
If Target.Row = 1 Then Exit Sub
If Target.Interior.ColorIndex <> [a1].Interior.ColorIndex Then Exit Sub
Cancel = True
If IsNumeric(Cells(Target.Row, Target.Column + 1)) = True Then
Cells(Target.Row, Target.Column + 1) = Cells(Target.Row, Target.Column + 1) + IIf(Cells(1, 1).Value = "双击计数,右键减少", -1, 1)
Else
If MsgBox("所选单元格计数框非数字,是否清空?", vbOKCancel) = 1 Then
Cells(Target.Row, Target.Column + 1) = 1
End If
End If
End Sub
运行是正常的。但是我还想加一个条件设置加减的上下限。比如加1的结果不高于5,等加到5的时候就始终显示5.比如减1的结果大于0,若是<=0的时候就赋值给它为" " (注意不是删除内容,否则我的条件格式就不能够保存了)
还有就是我始终觉得这段代码有点复杂?应该怎么组织语言简化一下呢? |
|