0% found this document useful (0 votes)
220 views

Difference Between .Delete and .Clear in Excel VBA - Stack Overflow

The key differences between .Delete and .Clear in Excel VBA are: - .Clear removes cell contents and formatting but does not delete the cell itself. .Delete removes the entire cell and shifts other cells to fill the gap. - .Clear preserves cell settings like borders and width but removes text and formatting. .Delete removes the entire cell including all settings, replacing it with a fresh cell. - There are additional .Clear methods like .ClearContents and .ClearFormats that clear only certain elements like contents or formatting while preserving others. .Delete fully removes the cell.

Uploaded by

vaskore
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
220 views

Difference Between .Delete and .Clear in Excel VBA - Stack Overflow

The key differences between .Delete and .Clear in Excel VBA are: - .Clear removes cell contents and formatting but does not delete the cell itself. .Delete removes the entire cell and shifts other cells to fill the gap. - .Clear preserves cell settings like borders and width but removes text and formatting. .Delete removes the entire cell including all settings, replacing it with a fresh cell. - There are additional .Clear methods like .ClearContents and .ClearFormats that clear only certain elements like contents or formatting while preserving others. .Delete fully removes the cell.

Uploaded by

vaskore
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

9/24/2020 Difference between .Delete and .Clear in Excel VBA?

- Stack Overflow

Difference between .Delete and .Clear in Excel VBA?


Asked 1 year ago Active 1 year ago Viewed 2k times

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

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

Dim delete as Range


Set delete = ThisWorkbook.Worksheets("Someting").Range("A1:D4")

delete.Clear 'It does clear my content


delete.Delete 'Does the same fucntion as .Clear? I mean It doesn't delete all the formatting
and width of the cells

Maybe Im writing It just wrong...

excel vba function

edited Sep 9 '19 at 7:26 asked Sep 9 '19 at 7:00


ChangeWorld
386 2 18

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

2 Answers Active Oldest Votes

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

edited Sep 9 '19 at 7:27 answered Sep 9 '19 at 7:13


buran
4,142 3 14 30

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:

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.

https://stackoverflow.com/questions/57849186/difference-between-delete-and-clear-in-excel-vba#:~:text=clear removes cell contents and,in to cover th… 2/3


9/24/2020 Difference between .Delete and .Clear in Excel VBA? - Stack Overflow
edited Sep 9 '19 at 7:43 answered Sep 9 '19 at 7:21
Plutian
2,117 3 11 22

https://stackoverflow.com/questions/57849186/difference-between-delete-and-clear-in-excel-vba#:~:text=clear removes cell contents and,in to cover th… 3/3

You might also like