Inserting / Deleting


Inserting Cells

Range("B2:F5").Insert(xlInsertShiftDirection.xlShiftDown) 
Range("B2:F5").Insert(xlInsertShiftDirection.xlShiftToRight)

Deleting Cells

This deletes the cells in the range "B2:F2".

Range("B2:F5").Delete(xlDeleteShiftDirection.xlShiftUp) 
Range("B2:F5").Delete(xlDeleteShiftDirection.xlShiftToLeft)

If the Shift argument is omitted then Excel will guess the best direction based on the shape of the range.


Clearing Data

ActiveCell.CurrentRegion.ClearContents 

Selection.Delete

Even with displayalerts = false you will get the following prompt if the selection currently has a filter applied to it.
SS
Delete entire row ?


Cutting and Pasting ranges of cells

You can cut and paste a single value using either of the following lines of code:

Range("A2").Cut Destination:=Range("D2") 
Range("A2").Cut Range("D2")

The Cut method also has an optional destination argument.
It is possible to cut and paste a range of cells:

Range("A2:B4").Select 
Selection.Cut
Range("D7").Select
ActiveSheet.Paste

Alternatively you can combine this to a single line.
This is possible because the Cut method can take an optional argument that can represent the range to be cut to. The next two lines are equivalent.

Range("A2:B4").Cut Destination:=Range("D7") 
Range("A2:B4").Cut Range("D7")

Application.Calculate

If you are manipulating a lot of cells then it is sometimes a good idea to switch the automatic calculation off temporarily

Application.Calculate = xlCalculation.xlCalculationManual 
'manipulating a lot of cells
Application.Calculate = xlCalculation.xlCalculationAutomatic

© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext