What is the difference between Worksheets(1).Cells.Delete
and 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!
Can anyone explain me the difference? And also can I give a range to .Delete
?
Range.Delete
actually delete respective range and shifts/move cells accordingly thus changing the structure of the worksheet. 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.
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
There is a variety of .clear
functions available in excel vba, tailored specifically to each situation and what you are trying to accomplish. 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.
In short, .clear
removes cell contents and certain types of formatting when specified. .delete
removes the entire cell and whatever formatting and shifts the rest of the documents in to cover the gap.
@ChangeWorld to answer your edited question: .delete
deletes the cell entirely, but shifts the cells around it to cover the gap. 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. .entirecolumn.delete
gets rid of the column and replaces it with the one on the right. Whatever cells replace the deleted ones, the settings and formatting from those cells will then apply.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.