跳到主要內容

 如何應用按鈕清除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 人工智慧助手:基於以下內容徹底改變數據分析: 智慧執行   |  生成代碼  |  建立自訂公式  |  分析數據並產生圖表  |  呼叫 Kutools 函數...
熱門特色: 尋找、突出顯示或識別重複項   |  刪除空白行   |  合併列或儲存格而不遺失數據   |   沒有公式的回合 ...
超級查詢: 多條件VLookup    多值VLookup  |   跨多個工作表的 VLookup   |   模糊查詢 ....
高級下拉列表: 快速建立下拉列表   |  依賴下拉列表   |  多選下拉列表 ....
欄目經理: 新增特定數量的列  |  移動列  |  切換隱藏列的可見性狀態  |  比較範圍和列 ...
特色功能: 網格焦點   |  設計圖   |   大方程式酒吧    工作簿和工作表管理器   |  資源庫 (自動文字)   |  日期選擇器   |  合併工作表   |  加密/解密單元格    按清單發送電子郵件   |  超級濾鏡   |   特殊過濾器 (過濾粗體/斜體/刪除線...)...
前 15 個工具集12 文本 工具 (添加文本, 刪除字符,...)   |   50+ 圖表 類型 (甘特圖,...)   |   40+ 實用 公式 (根據生日計算年齡,...)   |   19 插入 工具 (插入二維碼, 從路徑插入圖片,...)   |   12 轉化 工具 (數字到單詞, 貨幣兌換,...)   |   7 合併與拆分 工具 (高級合併行, 分裂細胞,...)   |   ... 和更多

使用 Kutools for Excel 增強您的 Excel 技能,體驗前所未有的效率。 Kutools for Excel 提供了 300 多種進階功能來提高生產力並節省時間。  點擊此處獲取您最需要的功能...

產品描述


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

  • 在Word,Excel,PowerPoint中啟用選項卡式編輯和閱讀,發布者,Access,Visio和Project。
  • 在同一窗口的新選項卡中而不是在新窗口中打開並創建多個文檔。
  • 將您的工作效率提高 50%,每天為您減少數百次鼠標點擊!
Comments (70)
Rated 5 out of 5 · 1 ratings
This comment was minimized by the moderator on the site
very helpful. thanks so much
This comment was minimized by the moderator on the site
Hi there,

Used this, and it works like a charm.

Only problem is... my cells are different formats... Some are 0,000 some are 00,00€ and when I it CLEAR, the format goes with tue clear event.

How can I clear the cells but maintain the format of them?
This comment was minimized by the moderator on the site
Hello, David,
To keep the cell formatting when clearing the cell contents, please apply the below code:
Sub ClearCellsKeepFormat()
    'Update by Extendoffice
    'This version clears the contents but keeps the cell formatting intact.
    Range("A2:A5").ClearContents
    Range("C10:D18").ClearContents
    Range("B8:B12").ClearContents
End Sub


Please have a try, hope it can help you!
This comment was minimized by the moderator on the site
It worked. Thanks a lot man 😄
Rated 5 out of 5
This comment was minimized by the moderator on the site
Hi how do you use this macro without interfering the data validation?
This comment was minimized by the moderator on the site
I wanted that the confirmation needed for clear button .....so that the data is not cleared by mistakenly press the "clear" tab

And i wanted to say thanks for your brilliant work....thanks a lot in advance....

wish you have a great day...
This comment was minimized by the moderator on the site
Hello,
I am using this on one spreadsheet, but on 3 separate tabs. Everything was working last time I had the workbook open but today it is giving me an error "Ambiguous name detected: Clearcells". Please help!
This comment was minimized by the moderator on the site
Hallo zusammen,
hab soweit alles gemacht zu gewiesen usw. Funktioniert auch aber sobald ich die Datei öffne funktioniert es nicht mehr und muss alles neu eingeben.

Vorab vielen Dank
This comment was minimized by the moderator on the site
Meinte, nach dem schließen und dann wieder öffne.
This comment was minimized by the moderator on the site
Hi. I am trying to use a toggle button to clear all cells of color and text, but only the cells that contain the word “event”. And then I’d like to use the same toggle button to add the color and the word “event” back onto the spreadsheet. Can this be done?
This comment was minimized by the moderator on the site
Hallo,

Hoe kan ik cellen in een range met een formule intact houden met bovenstaand script?

Mvg,

Nol
This comment was minimized by the moderator on the site
Hello, Nol,
Do you mean to keep the formula cells when clearing the cell contents? Could you explain your problem by using English?

Thank you!
This comment was minimized by the moderator on the site
Hi Skyyang,

Thats what I mean indeed

Regards,

Nol
This comment was minimized by the moderator on the site
Hi, Nol,,
Maybe the follwoing VBA code can help you:
Sub Clearcells()
Dim xStr As String
Dim xRgs, xRg, xRgA As Range
xStr = "A2:C3,A5:E16,F5:H10"
Set xRgs = Range(xStr)
For Each xRgA In xRgs.Areas
    For Each xRg In xRgA
        If Not xRg.HasFormula Then
            xRg.Clear
        End If
    Next
Next
End Sub


Please change the cell references to your need, and you should use the comma to separate the data range.
Please have a try, hope it can help you!
This comment was minimized by the moderator on the site
Thanks Skyyang, the script works like an charm! Keep up the good work
This comment was minimized by the moderator on the site
Can you help me find a code that would clear the contents of merged cells but keep the formatting as well as formulas?
Thank you
This comment was minimized by the moderator on the site
¡Hola! Este código es fantástico para mí: Sub Clearcells() Rango("A2", "A5").ClearContents Rango("C10", "D18").ClearContents Rango("B8", "B12").ClearContents End Sub
Sin embargo, quisiera que me indicarais, si sois tan amables, cómo puedo a la vez que borro el contenido de dichas celdas, que éstas no tuvieran ningún color de relleno, el estilo fuera el normal, sin negrita ni cursiva y la fuente fuera arial 10 . Muchísimas gracias de antemano!!
This comment was minimized by the moderator on the site
Bonjour, j'ai créé un classeur pour une compétition sportive, 2 feuilles, une pour les éliminatoires et une pour les parties finales, j'ai créé un bouton par feuilles pour effacer le contenu des cellules, je voudrais protéger ces boutons, ils ne doivent être utilisés qu'à la fin de la compétition.
Merci d'avance
Didier
This comment was minimized by the moderator on the site
Hello Didier,

How are you. You can create two buttons for the two worksheets, each button for each worksheet. Then you can save the workbook. You can decide which contents to be cleared in which worksheets. Have a nice day.

Sincerely,
Mandy
There are no comments posted here yet
Load More
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations