跳到主要内容

 如何应用按钮清除Excel中的特定单元格?

通常,您可以按住 按Ctrl 键选择多个特定的单元格,然后根据需要清除单元格内容。 如果您始终需要不时地清除这些特定的单元格,则可以创建一个清除按钮来一键清除它们。 在本文中,我将讨论如何创建一个全部清除按钮来清除某些特定的单元格内容。

应用按钮以清除带有VBA代码的特定单元格内容


箭头蓝色右气泡 应用按钮以清除带有VBA代码的特定单元格内容

首先,您应该创建一个形状按钮,然后应用代码,最后将代码绑定到形状按钮。 请执行以下操作:

1。 点击 插页 > 形状 > 矩形 选择“矩形”形状,然后根据需要拖动鼠标在图纸的任意位置绘制一个矩形按钮,请参见屏幕截图:

doc按钮清除单元格1

2。 然后输入文本并根据需要设置形状按钮的格式,请参见屏幕截图:

doc按钮清除单元格2

3。 然后您应该插入VBA代码,请按住 ALT + F11 键打开 Microsoft Visual Basic应用程序 窗口。 点击 插页 > 模块,然后将以下代码粘贴到 模块 窗口。

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 Tab 为 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