提示:其他語言是Google翻譯的。 你可以訪問 English 版本。
登入
x
or
x
x
註冊
x

or

如何清除Excel中另一個單元格的值更改指定的單元格內容?

假如你想清除一個指定單元格內容的範圍,如果另一個單元格的值發生了變化,你怎麼辦? 這篇文章會告訴你一個解決這個問題的方法。

如果另一個單元格的值用VBA代碼改變,請清除指定的單元格內容

Office選項卡在Office中啟用選項卡式編輯和瀏覽,使您的工作更輕鬆......
Kutools for Excel解決了您的大多數問題,並使您的生產率提高了80%
  • 重用任何東西: 將最常用或最複雜的公式,圖表和其他任何內容添加到您的收藏夾中,並在將來快速重複使用它們。
  • 超過20文本功能: 從文本字符串中提取數字; 提取或刪除部分文本; 將數字和貨幣轉換為英語單詞...
  • 合併工具:多個工作簿和表格合二為一; 合併多個單元格/行/列而不丟失數據; 合併重複行和總和...
  • 拆分工具:根據價值將數據拆分為多個表格; 一個工作簿到多個Excel,PDF或CSV文件; 一列到多列......
  • 粘貼跳過 隱藏/過濾行; 數和總和 按背景顏色; 創建郵件列表和 通過Cell的價值發送電子郵件...
  • 超級過濾器: 創建高級過濾方案並應用於任何工作表; 分類 按週,日,頻率等; 過濾 通過大膽,公式,評論......
  • 超過300強大的功能; 與Office 2007-2019和365一起使用; 支持所有語言; 在您的企業或組織中輕鬆部署。

如果另一個單元格的值用VBA代碼改變,請清除指定的單元格內容


如下圖所示,當單元格A2中的值改變時,單元格C1:C3中的內容將被自動清除。 請做如下。

1。 在工作表中,您將根據另一個單元格更改清除單元格內容,右鍵單擊工作表選項卡並選擇 查看代碼 從上下文菜單。 看截圖:

2。 在開幕式上 Microsoft Visual Basic for Applications 窗口中,將VBA代碼複製並粘貼到代碼窗口中。

VBA代碼:清除指定的單元格內容,如果另一個單元格的值發生變化

Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Range("A2")) Is Nothing Then
        Range("C1:C3").ClearContents
    End If
End Sub

注意:在代碼中,B2是您將根據其清除單元格內容的單元格,而C1:C3是您將清除內容的範圍。 請根據需要更改它們。

3。 按 其他 + Q 鍵關閉 Microsoft Visual Basic for Applications 窗口。

然後您可以看到範圍C1中的內容:當單元格A3中的值更改如下所示的截圖時,C2自動清除。


相關文章:


Kutools for Excel解決了您的大多數問題,並使您的生產率提高了80%

  • 重用: 快速插入 複雜的公式,圖表 以及你以前用過的任何東西; 加密單元格 密碼; 創建郵件列表 並發送電子郵件...
  • 超級方程式酒吧 (輕鬆編輯多行文字和公式); 閱讀佈局 (輕鬆讀取和編輯大量單元格); 粘貼到過濾範圍...
  • 合併單元格/行/列 不丟失數據; 分裂細胞含量; 組合重複的行/列...防止重複的細胞; 比較範圍...
  • 選擇複製或唯一 行; 選擇空行 (所有細胞都是空的); 超級查找和模糊查找 在許多工作簿中; 隨機選擇......
  • 精確複製 多個單元格而不更改公式參考; 自動創建參考 多張表; 插入項目符號,複選框等等......
  • 提取文本,添加文本,按位置刪除, 刪除空間; 創建和打印分頁小計; 在單元格內容和註釋之間轉換...
  • 超級過濾器 (將過濾方案保存並應用到其他工作表); 高級排序 按月/週/日,頻率等; 特殊過濾器 用粗體,斜體......
  • 結合工作簿和工作表; 根據鍵列合併表; 將數據拆分為多個表格; 批量轉換xls,xlsx和PDF...
  • 超過300強大的功能。 支持Office / Excel 2007-2019和365。 支持所有語言。 在您的企業或組織中輕鬆部署。 全功能30天免費試用。
kte tab 201905

Office選項卡為Office提供選項卡式界面,使您的工作更輕鬆

  • 在Word,Excel,PowerPoint中啟用選項卡式編輯和閱讀,Publisher,Access,Visio和Project。
  • 在同一窗口的新選項卡中打開並創建多個文檔,而不是在新窗口中。
  • 通過50%提高您的工作效率,每天為您減少數百次鼠標點擊!
官方底部
Say something here...
symbols left.
You are guest ( Sign Up? )
or post as a guest, but your post won't be published automatically.
Loading comment... The comment will be refreshed after 00:00.
  • To post as a guest, your comment is unpublished.
    Stephen · 1 months ago
    What about one to just clear the contents of any cell on a doubleclick?
  • To post as a guest, your comment is unpublished.
    Matt · 2 months ago
    What VBA code do I use if I have a table and need multiple blanks?

    My table is B3:E7. If the contents in column B is cleared then I would like to have the data in column C, D, E cleared for that row. I have the below for row 3, but would like the same in row 4,5,6 and 7.

    Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Range("B3")) Is Nothing Then
    Range("C3:E3").ClearContents
    End If
    End Sub
  • To post as a guest, your comment is unpublished.
    Sonia · 5 months ago
    any way of doing this without VBA?
    • To post as a guest, your comment is unpublished.
      crystal · 3 months ago
      Hi Sonia,
      Didn't find any solution exccept for VBA. Thanks for your comment.
  • To post as a guest, your comment is unpublished.
    GQ · 7 months ago
    In the case of A2, how do i reference a cell from a different worksheet?
  • To post as a guest, your comment is unpublished.
    Mac · 1 years ago
    Hi this isnt working for mine. Nothing changes but no errors either. Any tips?
    • To post as a guest, your comment is unpublished.
      crystal · 1 years ago
      Good day,
      Sorry for the inconvenience. Would you provide your Office version? Thanks for your comment.
  • To post as a guest, your comment is unpublished.
    Jason · 1 years ago
    Hi, I'm looking for a way to clear a range of cells of data when an "x" is entered in a certain cell. I used the above formula and it worked perfectly for that one row. The problem is that I need to extend it down to many rows. For example, if an "x" is entered in "D13", I need the range J:13 - v:13 to be cleared. I also need that to happen if an x is entered in "D14" as in I need the range d:14 - v:14 to be cleared. Is there a way to write that? Thanks for your help!
  • To post as a guest, your comment is unpublished.
    Mat 257 · 1 years ago
    hello, this works for a fixed source cell only (A2), how do this dynamically such as source is a variable cell? i tried to write
    A=activecell.row
    If Not Intersect(Target, Range("A"& A)) Is Nothing Then
    Range("C1:C3").ClearContents
    End If

    this should do the job with regard to activecell (ie selection) but is not working
    thanks
    • To post as a guest, your comment is unpublished.
      crystal · 1 years ago
      Good Day,
      Please try the below VBA code. Thank you for your comment.

      Private Sub Worksheet_Change(ByVal Target As Range)
      If (Not Intersect(Target, Rows(1)) Is Nothing) And (Target.Count = 1) Then
      Range("C1:C3").ClearContents
      End If
      End Sub
  • To post as a guest, your comment is unpublished.
    Scott · 1 years ago
    The code to clear a cell if another changes works great!!!! But I need it to work the other way around..... How is that code written??


    Thanks for your help
    • To post as a guest, your comment is unpublished.
      crystal · 1 years ago
      Dear Scott,
      What do you mean work the other way around? When manually clear contents of certain cells (C1:C3), then clear content of cell A2 automatically?