Difference Between .Delete and .Clear in Excel VBA - Stack Overflow
Difference Between .Delete and .Clear in Excel VBA - Stack Overflow
- Stack Overflow
3 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 ?
1
UPDATE
Why if I do
1 .clear is kind of the equivalent of clicking into a cell and pressing backspace until the content is gone.
This gets rid of the text and formatting, but nothing else, the cell settings are still there. .delete is a bit
more rigorous than that, it gets rid of the entire cell altogether and replaces it with a completely new one. All
formatting, settings, and content are gone, and you start with a fresh cell. – Plutian Sep 9 '19 at 7:10
So .Delete basically deletes the entire cell and replace it with a new one? But there is someting strange
with this function I mean... Wait let me edit the post. – ChangeWorld Sep 9 '19 at 7:15
@Plutian What is the difference between cell settings and formatting? Genuine question. – QHarr Sep 9 '19
at 7:18
@QHarr formatting is all things that determine how the displayed text is shown. Think Bold Italic or cell
colour . Cell settings are more like <- cell with ->, conditional formatting, and the specified "number
formatting". See my answer for documentation of different .clear varieties that clear certain types of
settings and formatting depending on your situation. – Plutian Sep 9 '19 at 7:29
2 @QHarr Perhaps the difference isn't that big, since the words are virtually synonymous, there would be
several experts using either word to explain the same thing. I personally think of the difference more like:
"Formatting" can be done within the cell, you can have Bold text next to normal text. "Settings" are things
that influence the entire cell, like border and with, background colour and the like. You can't have a bit of a
https://stackoverflow.com/questions/57849186/difference-between-delete-and-clear-in-excel-vba#:~:text=clear removes cell contents and,in to cover th… 1/3
9/24/2020 Difference between .Delete and .Clear in Excel VBA? - Stack Overflow
cell displaying it, its either all or nothing. As I said, see my or @buran answers to see .clear handles that
tailor specifically to what you are trying to do. – Plutian Sep 9 '19 at 7:36
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
8 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
3 this page Here is a list of the several varieties:
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.