首页 > 解决方案 > VBA-双击将x添加到单元格,但如何双击删除?

问题描述

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

    Set rInt = Intersect(Target, Range("B3:CS71"))
    If Not rInt Is Nothing Then
        For Each rCell In rInt
            rCell.Value = "x"
        Next
    End If
    Set rInt = Nothing
    Set rCell = Nothing
    Cancel = True
End Sub

我找到了一些示例代码,但它们似乎非常适合情况并专注于我无法操作的值,但只是看看是否有其他人看到“双击添加 x,双击删除 x”,以及我如何在这里应用“删除”部分。

提前致谢。

标签: excelvba

解决方案


只需If在循环中添加一条语句。试试这个:

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

  Set rInt = Intersect(Target, Range("B3:CS71"))
  If Not rInt Is Nothing Then
      For Each rCell In rInt
          If rCell.Value = "" then
            rCell.Value = "x"
          Else
            rCell.Value = ""                
          End If
      Next
  End If
  Set rInt = Nothing
  Set rCell = Nothing
  Cancel = True
End Sub

推荐阅读