Results 1 to 9 of 9

Thread: Not clearing contents in hidden cells

  1. #1
    VBAX Contributor
    Joined
    Oct 2004
    Location
    Cardiff
    Posts
    120
    Location

    Not clearing contents in hidden cells

    Hiya!

    Y'know if you highlight a range of cells that also includes a few hidden columns, then you clear the contents - you clear the contents in the hidden columns (or rows) also... Is there any code I can put in the sheet that will only clear the visible cells when the user hits the delete key or right clicks and chooses clear?



    Thanks in advance
    You cannot solve all the world's major problems using only potatoes. :- Adams

    http://www.vbaexpress.com

  2. #2
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    Try this code.

    Option Explicit
     
    Sub ChangeDels()
    Application.OnKey "{Del}", "NewDel"
        Application.CommandBars("Cell").Controls("Clear Contents").OnAction = "NewDel"
    End Sub
     
    Sub ResetDels()
    Application.OnKey "{Del}"
        Application.CommandBars("Cell").Controls("Clear Contents").OnAction = ""
    End Sub
     
    Sub NewDel()
    Dim DelRange        As Range
    On Error Resume Next
        Set DelRange = Selection.SpecialCells(xlCellTypeVisible)
        On Error GoTo 0
    If Not DelRange Is Nothing Then
            DelRange.ClearContents
        End If
    End Sub
    You can run the code to change the functions on the Workbook Activate event and run the code to reset it on the Workbook Deactivate event.

  3. #3
    VBAX Contributor
    Joined
    Oct 2004
    Location
    Cardiff
    Posts
    120
    Location
    That's cool Jake!

    It works great, just wish it wouldn't stop the user's ability to "undo/redo" - but you can't have everything
    You cannot solve all the world's major problems using only potatoes. :- Adams

    http://www.vbaexpress.com

  4. #4
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    Losing Undo is a small price to pay for keeping your data from being deleted.


  5. #5
    VBAX Contributor
    Joined
    Oct 2004
    Location
    Cardiff
    Posts
    120
    Location
    This is true, very true!

    Consider it solved - and thanks again.
    You cannot solve all the world's major problems using only potatoes. :- Adams

    http://www.vbaexpress.com

  6. #6
    VBAX Contributor
    Joined
    Oct 2004
    Location
    Cardiff
    Posts
    120
    Location
    Oh Darn

    This works fine until the user selects just one cell. Then, when he hits delete EVERYTHING that was in any visible cell gets wiped!
    You cannot solve all the world's major problems using only potatoes. :- Adams

    http://www.vbaexpress.com

  7. #7
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    Try this.
    [vba]
    Sub NewDel()

    Dim DelRange As Range

    If Selection.Cells.Count > 1 Then
    On Error Resume Next
    Set DelRange = Selection.SpecialCells(xlCellTypeVisible)
    On Error GoTo 0
    Else
    Set DelRange = Selection
    End If

    If Not DelRange Is Nothing Then
    DelRange.ClearContents
    End If

    End Sub
    [/vba]

  8. #8
    VBAX Contributor
    Joined
    Oct 2004
    Location
    Cardiff
    Posts
    120
    Location
    Perfect, thanks matey!!
    You cannot solve all the world's major problems using only potatoes. :- Adams

    http://www.vbaexpress.com

  9. #9
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    You're Welcome

    Take Care

Posting Permissions

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