• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Why Selection.Clear is not working

arishy

Member
Code:
Rows("19:23").Select
Selection.Delete Shift:=xlUp
Selection.Clear

By not working I mean the rows 19-23 are Still selected !!!

If I add
Range("A1").select

The selection goes away. i.e. cleared
 
Hi ,

If you go through the Excel help on Range.Clear , you will see that the Clear method does not remove the selection ; what ever was selected will continue to remain selected.

In fact the last statement :

Selection.Clear

will do something unintended ; you will see this if you open a new worksheet , enter some values in rows 19 through 27 , and then execute the above 3 lines of code. You can see that after deleting the rows 19 through 23 , data in rows 24 and below will be cleared , since Shift Up moves these rows to within the rows which are still selected.

Narayan
 
Hi Arishy,
  • Selection.Delete Shift:=xlUp
Select any area in Excel sheet, and press Ctrl + (-) , Ctrl Plus Minus
It will delete the cell, and ask you, how you want to fill the structure of the sheet, after deleting cell's from its location. if you select xlUP, the cell's below the selected cell, will come UPward, to fill the deleted area.

  • Selection.Clear
Select any area, and press Delete key. The cell's will still in its position, and the data in the cell, will be cleared. Nothing changes in the structure of the sheet. Simply clear the data in the cell.

Now, if you are in myth, that, selection.Clear is used to clear your selection, then you are in huge Misunderstanding. You cant de-select all cell, you have to select atleast a single cell to de-select any other cell. If you add Range("A1").select at the next line, you are selecting a cell, by leaving the other selected cell.

Please clarify, what exactly you want after Removing cell / deleting cell content
 
All I have to do is Rows("19:23").Delete

In trying to answer Debrai.....
I am copying FORMATED cells from another sheet. These cells had background color
When I tried to clear the CONTENT, the BG color REMAINED in the cells !!!
So, I used delete Rows to get rid of the BG color Rows. Stepping through the VBA I noticed the selected rows to be deleted were deleted ok, but the selection (grey area remained ).
THAT .......triggered my post.
 
Hi, arishy!

Summary: Excel does what it's told to do (not always but in this case yes).

<Range>.ClearContents will clear contents (values, formulas) but not cell formatting
<Range>.ClearFormats will do the inverse
<Range>.Delete will remove the range from the worksheet, shifting left or up.

Regards!
 
Hi, arishy!
Glad you solved it. Thanks for your feedback but credit to previous posters, I just resumed it. And welcome back whenever needed or wanted.
Regards!
 
Back
Top