Note: The other languages of the website are Google-translated. Back to English
登入  \/ 
x
or
x
註冊  \/ 
x

or

 如何應用按鈕清除Excel中的特定單元格?

通常,您可以按住 按Ctrl 鍵選擇多個特定的單元格,然後根據需要清除單元格內容。 如果您始終需要不時地清除這些特定的單元格,則可以創建一個清除按鈕來一鍵清除它們。 在本文中,我將討論如何創建一個全部清除按鈕來清除某些特定的單元格內容。

應用按鈕以清除帶有VBA代碼的特定單元格內容


箭頭藍色右氣泡 應用按鈕以清除帶有VBA代碼的特定單元格內容

首先,您應該創建一個形狀按鈕,然後應用代碼,最後將代碼綁定到形狀按鈕。 請執行以下操作:

1。 點擊 插入 > 形狀 > 矩形 選擇“矩形”形狀,然後根據需要拖動鼠標在圖紙的任意位置繪製一個矩形按鈕,請參見屏幕截圖:

doc按鈕清除單元格1

2。 然後輸入文本並根據需要設置形狀按鈕的格式,請參見屏幕截圖:

doc按鈕清除單元格2

3。 然後您應該插入VBA代碼,請按住 ALT + F11 鍵打開 Microsoft Visual Basic for Applications 窗口。 點擊 插入 > 模塊,然後將以下代碼粘貼到 模塊 窗口。

VBA代碼:清除特定單元格中的單元格內容:

Sub Clearcells()
'Updateby Extendoffice
Range("A2", "A5").Clear
Range("C10", "D18").Clear
Range("B8", "B12").Clear
End Sub

備註:在上面的代碼中: A2,A5 表示它將清除範圍內的單元格 A2:A5,而不僅僅是兩個單元格,您可以添加多個範圍,例如 範圍(“ B8”,“ B12”)。清除 腳本內的代碼要清除。

4。 然後保存並關閉代碼窗口,然後將代碼鏈接到形狀按鈕,右鍵單擊該按鈕,然後選擇 分配宏分配宏 對話框中選擇 透明單元 的代碼名稱 宏名 列錶框,然後單擊 OK 按鈕退出此對話框。 看截圖:

doc按鈕清除單元格3

5。 現在,當您單擊 全部清除 按鈕,您定義的特定單元會立即清除,請參見屏幕截圖:

doc按鈕清除單元格4


最佳辦公效率工具

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

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

Office選項卡為Office帶來了選項卡式界面,使您的工作更加輕鬆

  • 在Word,Excel,PowerPoint中啟用選項卡式編輯和閱讀,發布者,Access,Visio和Project。
  • 在同一窗口的新選項卡中而不是在新窗口中打開並創建多個文檔。
  • 每天將您的工作效率提高50%,並減少數百次鼠標單擊!
officetab底部
Say something here...
symbols left.
You are guest
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.
    Sarah · 7 months ago
    So it clears the data just fine and keeps the formatting, but then I lose all my formulas in the cells. Is there anyway to fix this so it just clears the number?
    It's for data entry of rating scales
  • To post as a guest, your comment is unpublished.
    king · 8 months ago
    How can you do this for check boxes so they also clear in addition to the fields?
  • To post as a guest, your comment is unpublished.
    Manyusli · 8 months ago
    SUPER HELPFUL! THANK YOU.
    But I have another question, maybe you can help me, in order to create the Clear cell macro button, and I already did it, and it just work perfectly. But I need to protect the sheet to make sure nobody delete important formulas, but it turns that this action make the clearcell macro button unclickble.......
    What can I do in this case? Help please.....
    • To post as a guest, your comment is unpublished.
      Manyusli · 8 months ago
      This is a screenshot of my work sheet, sorry I'm not a expert in excel....
  • To post as a guest, your comment is unpublished.
    Monica · 8 months ago
    SUPER HELPFUL! THANK YOU.
    But I have another question, maybe you can help me, in order to create the Clear cell macro button, and I already did it, and it just work perfectly. But I need to protect the sheet to make sure nobody delete important formulas, but it turns that this action make the clearcell macro button unclickble.......
    What can I do in this case? Help please.....
  • To post as a guest, your comment is unpublished.
    nate · 8 months ago
    i am trying to clear certain cells across several tabs. How can i accomplish this? I have built a checklist that is about 100 questions long on several tabs, and it is hard to go back and delete the feedback from every cell.
  • To post as a guest, your comment is unpublished.
    Robin · 1 years ago
    Hi! Works great for me with this code as you mentioned:

    Sub Clearcells()
    Range("A2", "A5").ClearContents
    Range("C10", "D18").ClearContents
    Range("B8", "B12").ClearContents
    End Sub

    However, I want to keep the number "0" or the procentage "0" for certain cells. Also, keep the previous selected colour. The reson for this is I use these cells as input cells and want to use the buttom to clear certin input cells.


    Thanks in advance!
  • To post as a guest, your comment is unpublished.
    walaseyhk@gmail.com · 1 years ago
    Please give marco for clear if cell content less than 4 digital numbers. Thanks
  • To post as a guest, your comment is unpublished.
    jorge · 1 years ago
    when the command button I press delete it also deletes the continuous cells and I already have a formula in them and I don't want to delete those
  • To post as a guest, your comment is unpublished.
    Arshpreet · 1 years ago
    its not working
  • To post as a guest, your comment is unpublished.
    nocktys.fbm@gmail.com · 1 years ago
    How to do on Google Sheets please ?
  • To post as a guest, your comment is unpublished.
    Fabricio · 1 years ago
    It is not working on merged cells. :(
    • To post as a guest, your comment is unpublished.
      Penny · 1 years ago
      Try this,

      For example: The merged cells are columns A and B. You want to clear rows 2 thru 5. It'd look like the below.

      Sub Clearcells()
      Range("A2:B2", "A5:B5").ClearContents
      End Sub
    • To post as a guest, your comment is unpublished.
      taviodnw@gmail.com · 1 years ago
      replace .Clear by = ""
      • To post as a guest, your comment is unpublished.
        Kean Miller · 1 years ago
        For merged cells do you mean have the formatting for example be Range("A25","B25","C25")="" ?
  • To post as a guest, your comment is unpublished.
    ljmajik@gmail.com · 1 years ago
    Is there a way for me to keep the data validation list drops on the cells. For example my list drop includes Yes or No and I would like to reset the cells to blank but keep my data validation in the background
    • To post as a guest, your comment is unpublished.
      Don · 1 years ago
      you can use = clear.


      for Eg.

      Range("A2", "A5") = Clear
  • To post as a guest, your comment is unpublished.
    Jennifer · 1 years ago
    Hi,

    What code do I use if I only need to clear one single cell instead of a range of cells?
    • To post as a guest, your comment is unpublished.
      skyyang · 1 years ago
      Hi, Jennifer,
      To clear only one cell, you just need to change the VBA code as this: (Note: change the cell A2 to the cell you want to clear)

      Sub Clearcells()
      Range("A2").Clear
      End Sub

      Please try, hope it can help you!
  • To post as a guest, your comment is unpublished.
    Radheshyam · 2 years ago
    simply I did same in worksheet it worked when I protect the sheet it shows error 1004, any suggestion in this case?
    • To post as a guest, your comment is unpublished.
      skyyang · 2 years ago
      Hi, Radheshyam,
      To run above code in a protect worksheet, please apply the below code: (Note: change the text "password" to the password which protect your sheet)
      Sub ClearcellsAsProtect()
      Dim xWS As Worksheet
      Dim xPsw As String
      Set xWS = ActiveSheet
      xPsw = "password"
      On Error Resume Next
      xWS.Unprotect Password:=xPsw
      Range("A2", "A5").Clear
      Range("C10", "D18").Clear
      Range("B8", "B12").Clear
      xWS.Protect Password:=xPsw
      End Sub

      Please try, hope it can help you!
  • To post as a guest, your comment is unpublished.
    Stan · 2 years ago
    I have a workbook with 11 tabs, plus one at the beginning marked as "Start Here". I want to create one button in that tab that will clear up to 9 individual cells of it's contents, or enter a "0" in it within each of these tabs. The cells I want to clear do not necessarily reside in the same spot on each page. Is this possible and how is it done? I presume this can be done in VisualBasic, but would it be easier for a neophyte to create it using Macros?
    • To post as a guest, your comment is unpublished.
      Nate · 8 months ago
      i have the same question

      • To post as a guest, your comment is unpublished.
        Christopher · 6 months ago
        Use this as a script template:

        Sub Clearcells()
        'Updateby Extendoffice 20161008
        Range("b11:d22").ClearContents 'this line refers to the page with the macro button.
        Range("'Eval Score Entry'!D2:AA2").ClearContents 'this line refers to a different tab and range.
        End Sub


  • To post as a guest, your comment is unpublished.
    Adnandos · 2 years ago
    Hi. What is the best way to create seperate buttons to clear the contents of each row separately? eg data capturer is happy with the inputs of every other row, but then needs to clear rows 3, row 6 and row 7. What's the most efficient way to create buttons to clear in this way, ie row only?
    • To post as a guest, your comment is unpublished.
      OS · 2 years ago
      Hi, Put a button on top of row 3,6,7. Assign the same marco as above on each button to clear that particular row only.
  • To post as a guest, your comment is unpublished.
    basilrubber@gmail.com · 3 years ago
    This is clearing the border also, what can i do?
    • To post as a guest, your comment is unpublished.
      Jay · 2 years ago
      change clear to clearcontents then formatting will stay the same after clearing. only text will be gone not borders and colouring etc
  • To post as a guest, your comment is unpublished.
    ian · 3 years ago
    can you make this work over several sheets?
  • To post as a guest, your comment is unpublished.
    Piotr · 3 years ago
    Thx for this code. How to clear cell contents or ideally fill it up with 0 value in a protected sheet with just few editable cells? Thx for help on ot.
    • To post as a guest, your comment is unpublished.
      Robert · 3 years ago
      Instead of the clear command, an example line from above would look like:
      Range("A2:A5") = 0
  • To post as a guest, your comment is unpublished.
    Ashleigh Bonner · 3 years ago
    Thank you so much for this information!!

    Another tip: I added a textbox. To combine the textbox with the shape, I selected the textbox, held down SHIFT, and then selected the shape. With those both selected I right-clicked and selected GROUP, then GROUP, again.

    When these are grouped, you can still change the text and other formatting.

    To ungroup, just right-click your new button and select GROUP > UNGROUP.
    • To post as a guest, your comment is unpublished.
      Calvin Hodgson · 2 years ago
      Or, simply right-clicking the shape allows you to change formatting and text.
  • To post as a guest, your comment is unpublished.
    Steph · 3 years ago
    thank you!
  • To post as a guest, your comment is unpublished.
    ken besse · 3 years ago
    This also clears any formatting of the cell. How doI do this if I wantto keep formulas and formatting suchace as shading or borders
    • To post as a guest, your comment is unpublished.
      Adam · 3 years ago
      change the command from "clear" to "clearcontents" to just erase the text inputs
      • To post as a guest, your comment is unpublished.
        dsnyan · 2 years ago
        what about the color?
  • To post as a guest, your comment is unpublished.
    anand · 3 years ago
    GIVE FORMULAS FOR SAVE AND CLEAR