+ Reply to Thread
Results 1 to 13 of 13

Delete cell and shift cell up

  1. #1
    mridzuan
    Guest

    Delete cell and shift cell up

    I have data in cells A1:A4. I want if cell A2 is deleted using the button delete, the cell will automatically shift up. Can a macro do this?

  2. #2
    Forum Contributor
    Join Date
    11-29-2003
    Posts
    1,203
    Yes. You want an event procedure. Do you want this apply to:
    1. just one worksheet?
    2. or every worksheet in one workbook?
    3. or every workbook open at the time?

    1 & 2 are pretty simple. You write a worksheet_change event macro either on the worksheet's code window (case 1), or the workbook's code window (case 2). http://www.cpearson.com/excel/events.htm

    3 is more difficult, as it requires "application events", which do not come "built-in". You would need to write a ClassModule to create application events. http://www.cpearson.com/excel/AppEvent.htm

  3. #3
    mridzuan
    Guest
    I need it just for one worksheet.

  4. #4
    Forum Contributor
    Join Date
    11-29-2003
    Posts
    1,203
    Just one column or all columns on that sheet?

  5. #5
    mridzuan
    Guest
    For a range of columns in the worksheet

  6. #6
    Forum Contributor
    Join Date
    11-29-2003
    Posts
    1,203
    OK, since you won't tell me, and I can't seem to drag it out of you ,,, here is the code if the "range of columns" you happen to want are B:J (2 through 10).

    If you happen to want a different range of columns, change the numbers in the first 2 IF statements.


    Please Login or Register  to view this content.
    To find the right place to put this code, select the tab for the worksheet you want this to work for; right-click, from the context menu select "View Code" and paste this where the cursor is flashing.

  7. #7
    mridzuan
    Guest
    I'm sorry if you misunderstood me. Let me explain it in full. In Sheet1, I have data in cells A1:A10. What I wanted is if I were to delete cell A2 using the "Delete" button on the keyboard, cell A3 will shift up.

  8. #8
    Forum Contributor
    Join Date
    11-29-2003
    Posts
    1,203
    So, if I ask this question again:
    Just one column or all columns on that sheet?
    Your answer is "just column A", right?

    In that case, the code is:
    Please Login or Register  to view this content.
    Or, if I am still confused ... draw me a picture. I am slow sometimes (I mean it!)

  9. #9
    mridzuan
    Guest
    Thanks. It works just fine for the whole column. What needs to be done if I want the Macro to work only on a range of cells like I mentioned before?

  10. #10
    Forum Contributor
    Join Date
    11-29-2003
    Posts
    1,203
    So ,,, I told you I can be slow at times.

    cells A1:A10 right?

    Please Login or Register  to view this content.
    Do you want the cells from A11 down to stay in place? That would require a few more lines of code.

  11. #11
    Forum Contributor
    Join Date
    11-29-2003
    Posts
    1,203

    Oops ...

    I noticed a slight problem with the code I posted earlier. Namely, if the next cell happends to be also be blank, it never stops until to gets to a non-blank cell.

    Code below fixed that. And, if you want rows 11+ to stay in place, remove the tic-mark (') from the row that is green.

    Please Login or Register  to view this content.

  12. #12
    mridzuan
    Guest
    It works perfectly as I needed. A million thanks to you.

  13. #13
    Forum Contributor
    Join Date
    11-29-2003
    Posts
    1,203
    Thanks for the feedback!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1