Enforcing Moving Cells Up

Written by Allen Wyatt (last updated November 20, 2023)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and 2021


Chris has, at work, a big worksheet that includes several macros. One of these deletes a block of cells and moves the rest up. Sometimes, though, users will not use the macro and, instead, manually delete the block. It is a disaster if the deletion moves cells to the left instead of up. Chris wonders if there is a way to use the macros to enforce the upward moving of cells and preclude the manual movement of cells to the left.

As with many things in Excel, there are multiple ways that you can approach this problem. I cannot go too deep into specifics here, as any approach ultimately selected will depend on the nature of the information in the worksheet and what you want to allow or disallow. The following sections, however, may provide the key that you need to pick the approach that is best for you.

Protect the Worksheet

A simple (and effective) approach is to design your worksheet so it can be protected. Of course, you would make sure that any cells where the user should enter information are unprotected, but otherwise your macro can make sure that when the worksheet is activated, it is protected. This precludes the possibility of the user manually deleting any cells from the worksheet.

This means that if the range of cells does need to be deleted, the user is forced (by the protection) to select your macro for deletion. The macro can then unprotect the worksheet, delete the range of cells, and reprotect it.

Intercepting Deletes

Another macro-based approach is to work with the BeforeDelete event to control what actually happens. The following is a simple event handler that could be the basis of what you actually use:

Private Sub Worksheet_BeforeDelete()
    Dim delRng As Range
    Dim selRng As Range

    On Error Resume Next
    Set delRng = Range("C2:D5")  ' Change the range to the block
                                 ' of cells to be deleted (manually
                                 ' or by macro)
    Set selRng = Application.Selection  ' Cells selected manually
                                        ' by the user
    If MyRng.Address = delRng.Address Then
        Selection.Delete shift:=xlUp
    End If
End Sub

The event handler simply compares the range you want to have deleted only upwards with the range the user selected to delete. If the ranges are the same, then the handler deletes the range and moves it up. You would need to, obviously, change the range assigned to the delRng variable to match the range you want to monitor.

Remapping the Delete Key

Speaking of deleting, you could also remap the Delete key so that it does whatever you want. All you need to do is to include a macro like the following in the ThisWorkbook code module:

Private Sub Workbook_Open()
    Application.OnKey "{DELETE}", "MyMacro()"
End Sub

This means that when the workbook is opened, the Delete key is remapped so that whenever it is pressed, MyMacro (change this to whatever macro you desire) is automatically run.

Note that this approach affects only the pressing of the Delete key; it does not affect deletions done in any other way. For those, your macros would still need to anticipate and handle the logic.

The Complete Approach

That brings us to the complete approach. This approach simply means making a list of all the various ways that the user could initiate a deletion and then intercepting those ways. The list (and the interceptions) would necessarily need to include some of the approaches already discussed, such as handling the Delete key. You would also need to figure out how to disable or modify what happens when the user right-clicks or when the user selects a delete option from the ribbon. (You might even want to remove deletion-related options from the user interface.)

This approach can be quite long, as an exhaustive list of deletion methods could be laborious to put together and handle.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (13632) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and 2021.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Inserting the Author Name

Did you know that Word tries to keep track of who the author of a document is? This information can be easily added to ...

Discover More

Differentiating a Header Row

When you use the sorting tool, Excel tries to automatically figure out if your data includes a header row or not. Here ...

Discover More

Changing the Line between Text and Footnotes

When your document includes footnotes, Word adds a line between the main text and those footnotes so that they are ...

Discover More

Best-Selling VBA Tutorial for Beginners Take your Excel knowledge to the next level. With a little background in VBA programming, you can go well beyond basic spreadsheets and functions. Use macros to reduce errors, save time, and integrate with other Microsoft applications. Fully updated for the latest version of Office 365. Check out Microsoft 365 Excel VBA Programming For Dummies today!

More ExcelTips (ribbon)

Understanding Subroutines

When developing macros, you can create subroutines. This is a great way to reuse common code and make your programming ...

Discover More

Stopping Excel from Deleting Macros from a Workbook

When working with very large workbooks, it is possible for Excel to behave erratically. This tip looks at ways you can ...

Discover More

Making Sure Numbers Copy as Numbers

When you copy information from one worksheet to another using a macro, you might not get exactly what you want. This tip ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is seven less than 7?

There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.