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应用程序 窗口。 点击 插页 > 模块,然后将以下代码粘贴到 模块 窗口。

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.
    Dave · 1 months ago
    My excel seems to be bringing up other workbook modules making it very difficult
    to know what's going on
  • To post as a guest, your comment is unpublished.
    Christopher · 9 months ago
    @Nate 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.
    Sarah · 10 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 · 10 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 · 10 months ago
    @Manyusli 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.
    Manyusli · 10 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.
    Monica · 10 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 · 10 months ago
    @Stan i have the same question

  • To post as a guest, your comment is unpublished.
    nate · 10 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.
    Penny · 1 years ago
    @Fabricio 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.
    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.
    karamahanna@gmail.com · 1 years ago
    @skyyang Thank you skyyang
    Now it work properly
  • To post as a guest, your comment is unpublished.
    Kean Miller · 1 years ago
    @taviodnw@gmail.com 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.
    skyyang · 1 years ago
    @karamahanna@gmail.com Hello, Karam,
    To only clear the data and keep the cell formatting, please apply the below code:

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

    Please try, hope it can help you!
  • To post as a guest, your comment is unpublished.
    taviodnw@gmail.com · 1 years ago
    @Fabricio replace .Clear by = ""
  • 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.
    Don · 1 years ago
    @ljmajik@gmail.com you can use = clear.


    for Eg.

    Range("A2", "A5") = Clear
  • 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.
    skyyang · 2 years ago
    @Jennifer 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.
    ljmajik@gmail.com · 2 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.
    Jennifer · 2 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 · 2 years ago
    @Radheshyam 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.
    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.
    OS · 2 years ago
    @Adnandos 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.
    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.
    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.
    Calvin Hodgson · 2 years ago
    @Ashleigh Bonner Or, simply right-clicking the shape allows you to change formatting and text.
  • To post as a guest, your comment is unpublished.
    Jay · 3 years ago
    @basilrubber@gmail.com 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.
    dsnyan · 3 years ago
    @Adam what about the color?
  • 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.
    ian · 3 years ago
    can you make this work over several sheets?
  • To post as a guest, your comment is unpublished.
    Robert · 3 years ago
    @Piotr 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.
    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.
    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.
    Steph · 3 years ago
    thank you!
  • To post as a guest, your comment is unpublished.
    Adam · 3 years ago
    @ken besse change the command from "clear" to "clearcontents" to just erase the text inputs
  • 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.
    anand · 3 years ago
    GIVE FORMULAS FOR SAVE AND CLEAR