vba 使用单个命令清除 Excel 单元格的内容和格式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31884818/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 09:52:41  来源:igfitidea点击:

Clear contents and formatting of an Excel cell with a single command

vbaexcel-vbaexcel-2013excel

提问by MackM

If you want to clear the contents of a cell or range in Microsoft Excel, you can use .ClearContents. If you also want to clear the formatting, you can use .ClearFormats.

如果要清除 Microsoft Excel 中单元格或区域的内容,可以使用.ClearContents. 如果您还想清除格式,可以使用.ClearFormats.

Sheets("Test").Range("A1:C3").ClearContents
Sheets("Test").Range("A1:C3").ClearFormats

If you want to do both, you can use .Delete, but then the other cells in the spreadsheet shift to replace the deleted cells.

如果您想同时执行这两项操作,则可以使用.Delete,但随后电子表格中的其他单元格会移动以替换已删除的单元格。

Sheets("Test").Range("A1:C3").Delete

How can you remove the contents andthe formatting of a cell or range in VBA with a single command, without affecting the rest of the worksheet?

如何使用单个命令在 VBA 中删除单元格或区域的内容格式,而不影响工作表的其余部分?

回答by MackM

Use the .Clearmethod.

使用.Clear方法。

Sheets("Test").Range("A1:C3").Clear

MSDN documentation here.

MSDN 文档在这里。