简体   繁体   English

Excel VBA中.Delete和.Clear的区别?

[英]Difference between .Delete and .Clear in Excel VBA?

What is the difference between Worksheets(1).Cells.Delete and Worksheets(1).Cells.Clear ? Worksheets(1).Cells.DeleteWorksheets(1).Cells.Clear什么Worksheets(1).Cells.Clear

I'm asking this because I've always used .Clear to clear my Worksheet content, but in my previous post I've discovered that Worksheets(1).Cells.Delete not only delete my Worksheet content but it also set Columns on their default width!我之所以这么问是因为我一直使用.Clear来清除我的工作表内容,但在我之前的帖子中,我发现Worksheets(1).Cells.Delete不仅删除了我的工作表内容,而且还在其上设置了列默认宽度!

Can anyone explain me the difference?谁能给我解释一下区别? And also can I give a range to .Delete ?我也可以给.Delete一个范围吗?

Range.Delete actually delete respective range and shifts/move cells accordingly thus changing the structure of the worksheet. Range.Delete实际上会删除相应的范围并相应地移动/移动单元格,从而改变工作表的结构。 It's same if you select cells and right-click to Delete selection.如果您选择单元格并右键单击以删除选择,则相同。 You get asked how do you want to move cells.你会被问到你想如何移动单元格。

Range.Clear will just clear the content and formatting, but will not delete cells and not change the structure of the worksheet. Range.Clear只会清除内容和格式,但不会删除单元格,也不会更改工作表的结构。

There is also Range.ClearContents that will clear just content, but preserve formatting, Range.ClearFormats to just clear formatting of the cells and Range.ClearComments , Range.ClearHyperlinks , Range.ClearNotes and Range.ClearOutline还有Range.ClearContents只清除内容,但保留格式, Range.ClearFormats只清除单元格和Range.ClearCommentsRange.ClearHyperlinksRange.ClearNotesRange.ClearOutline

There is a variety of .clear functions available in excel vba, tailored specifically to each situation and what you are trying to accomplish. excel vba 中有多种.clear函数可用,专门针对每种情况和您要完成的任务量身定制。 Some documentation and explanation can be found on this page Here is a list of the several varieties:一些文档和解释可以在这个页面上找到这里是几个品种的列表:

Clear            Remove formatting and contents 
ClearFormats     Remove cell formatting, including font format 
ClearComments    Remove comments 
ClearHyperlinks  Reomve hyperlinks, but the default format of hyperlink is not removed 
Clear Notes      Remove Notes 
ClearOutline     Remove Outline 
Delete           Remove the entire cell

An in-depth explanation of .delete behaviour can be found here.可以在此处找到.delete行为的深入解释

In short, .clear removes cell contents and certain types of formatting when specified.简而言之, .clear在指定时删除单元格内容和某些类型的格式。 .delete removes the entire cell and whatever formatting and shifts the rest of the documents in to cover the gap. .delete删除整个单元格和任何格式,并将其余文档.delete以填补空白。

@ChangeWorld to answer your edited question: .delete deletes the cell entirely, but shifts the cells around it to cover the gap. @ChangeWorld 回答您编辑的问题: .delete完全删除单元格,但移动它周围的单元格以覆盖间隙。 Like water flowing in a hole.如同洞中流淌的水。 The cell that replaces it can be below and have the same settings applied to it.替换它的单元格可以在下方,并对其应用相同的设置。 If you want the settings to disappear completely use .entirerow.delete which gets rid of the row and replaces it with the one below it.如果您希望设置完全消失,请使用.entirerow.delete删除该行并将其替换为下面的行。 .entirecolumn.delete gets rid of the column and replaces it with the one on the right. .entirecolumn.delete删除该列并将其替换为右侧的列。 Whatever cells replace the deleted ones, the settings and formatting from those cells will then apply.无论什么单元格替换已删除的单元格,这些单元格的设置和格式都将应用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM