VBA选中标记 - 如果双击并删除,则返回“是”

时间:2017-06-14 04:42:56

标签: excel-vba vba excel

我使用下面的VBA代码在双击单元格时显示复选标记/勾号。有时,需要删除复选标记/勾号。要删除复选标记/勾选,需要再次双击单元格。双击以删除复选标记/勾选后,单元格将返回空白。我的问题是;是否可以显示单词'YES'而不是将单元格留空?

或者作为一个更好的选择,当双击(删除复选标记/勾号)而不是返回空白时,是否可以在单元格中返回原始文本(在单元格被双击之前)?

我希望这是有道理的!提前谢谢!

以下是我用于复选标记/勾号的VBA代码:

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    On Error GoTo 1
    If Not Intersect(Target, Range("A2:Z200")) Is Nothing Then
        Application.EnableEvents = False
        If Target.Value = ChrW(&H2713) Then
            Target.ClearContents
            Cancel = True
        Else
            Target.Value = ChrW(&H2713)
            Cancel = True
        End If
    End If

    On Error GoTo 0
1   Application.EnableEvents = True

End Sub

2 个答案:

答案 0 :(得分:0)

要显示“是”字样,请更改以下行:

Target.ClearContents

使用:

Target.Value = "YES"

完整代码

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)

    On Error GoTo 1
    If Not Intersect(Target, Range("A2:Z200")) Is Nothing Then
        Application.EnableEvents = False
        If Target.Value = ChrW(&H2713) Then
            Target.Value = "YES"
        Else
            Target.Value = ChrW(&H2713)
        End If
        Cancel = True
    End If

    On Error GoTo 0
1   Application.EnableEvents = True

End Sub

答案 1 :(得分:0)

这将使用复选标记替换原始值,然后在重新单击时恢复原始值,但它仅适用于文本值,仅更改单元格的外观 ...

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)

    If Not Intersect(Target, Range("A2:Z200")) Is Nothing Then
        On Error GoTo haveError
        Application.EnableEvents = False
        If Target.NumberFormat Like ";;;*" Then
            Target.NumberFormat = "General"
        Else
            Target.NumberFormat = ";;;" & ChrW(&H2713)
        End If
        Cancel = True
    End If

haveError:
    Application.EnableEvents = True

End Sub