Consulting

Results 1 to 7 of 7

Thread: Solved: Deleting cell contents rather than cell itself

  1. #1

    Solved: Deleting cell contents rather than cell itself

    Hello again, I have the following code that deletes a cell once I click the linked button in the worksheet. What I was wondering, was is it possible to edit this so it only deletes the contents rather than the cell itself?

    Tanks,

    John.

    [vba]
    Sub deleteMe()
    ActiveSheet.Unprotect Password:="password"
    ActiveCell.Delete
    ActiveSheet.Protect Password:="password", DrawingObjects:=True
    End Sub
    [/vba]

  2. #2
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    Try this.
    [vba]
    Sub deleteMe()
    ActiveSheet.Unprotect Password:="password"
    ActiveCell.ClearContents
    ActiveSheet.Protect Password:="password", DrawingObjects:=True
    End Sub [/vba]

  3. #3
    Excellent Norie, just what I was looking for.

    Thank you very much.

  4. #4
    Hi, John! I'm having trouble today understanding what people are asking, so bear with me. What do you mean by "deleting a cell" rather than deleting its contents?

  5. #5
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Hi Sid,

    The code, as John had written it, would delete the entire cell, and the rest of the cells in the column would shift up.

    Try filling 3 rows by 3 columns with data. =rand() will fill it quite nicely. Then run the following line of code with one of those cells selected.

    [vba]Activecell.Delete[/vba]

    One of your columns of data will be a little shorter now.
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  6. #6
    Yes, the original code deleted the cell itself shifting the cells below up one, the 'Activecell.Clearcontents' just clears the cell leaving it blank.

    Hope this helps.

    John.

  7. #7
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Just for reference...

    Activecell.Clearcontents will clear the contents of the cell, but leaves the formatting intact. If you want to wipe everything (formats & values) from the cell, you could use:

    [vba]Activecell.Clear[/vba]

    I find that for most of my applications, that I prefer to leave the formatting of a cell alone once I've set it up. Clear does have it's uses sometimes, though.
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





Posting Permissions

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