Excel:如果单击其他单元格,则删除单元格内容

时间:2018-03-05 20:44:46

标签: excel excel-vba vba

我有以下代码,每次在单元格中单击它时在单元格上设置“x”(“C3:C40)”。

问题是它将“x”放在多个单元格中,我只希望在范围内只有一个“x”,只需要点击一个。

如何更改此代码才能生成?

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim rInt As Range
    Dim rCell As Range

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

由于

3 个答案:

答案 0 :(得分:1)

添加了一个循环,用于检查指定范围内的任何值,并将值设置为清除字符串(无)(如果找到)。然后,您使用的代码将“x”放在所需的单元格上。

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim rInt As Range
Dim rCell As Range
Dim c As Variant

Set rInt = Intersect(Target, Range("C3:C40"))
If Not rInt Is Nothing Then
    For Each c In Range("C3:C40").Cells 'Erase loop
        If c.Value <> "" Then
            c.Value = ""
        End If
    Next

    For Each rCell In rInt
        rCell.Value = "x"
    Next
End If
Set rInt = Nothing
Set rCell = Nothing
End Sub

答案 1 :(得分:1)

阐述我对原帖的评论:

Z0 <- X %o% rep(NA, length(unique(X)))

for(i in 1:length(Y)){  Z0[i, X[i]] <- Y[i] }

答案 2 :(得分:0)

如果范围值不是x,那么范围值是否为空?

   Private Sub Worksheet_SelectionChange(ByVal 
   Target As Range)
    Dim rInt As Range

    Set rInt = Intersect(Target, Range("C3:C40"))
    If Not rInt Is Nothing Then
        rlnt.value=""
        ActiveCell.Value = "x"
    End If
    End Sub