簡體   English   中英

Excel:如果單擊另一個單元格,則刪除單元格內容

[英]Excel: Delete cell content if another cell is clicked

我有以下代碼,每次在范圍(“ C3:C40)”中單擊它時,都會在單元格上設置“ x”。

問題是將“ 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

謝謝

添加了一個循環,該循環檢查指定范圍內的任何值,並將該值設置為清晰的字符串(無)(如果找到)。 然后,您一直在使用的代碼將“ 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

詳細說明我對原始帖子的評論:

Dim r as Long, c as Long
r = ActiveCell.Row 
c = ActiveCell.Column
Range("C3:C40").ClearContents
Cells(r,c).Value="x"

如果它不是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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM