+ Reply to Thread
Results 1 to 6 of 6

clear contents of entire sheet except the contents of the table

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    04-29-2012
    Location
    nuneaton, England
    MS-Off Ver
    Office 365
    Posts
    374

    clear contents of entire sheet except the contents of the table

    how could this be achieved please

  2. #2
    Valued Forum Contributor
    Join Date
    03-24-2014
    Location
    England
    MS-Off Ver
    Excel 2003 - 2016
    Posts
    575

    Re: clear contents of entire sheet except the contents of the table

    Select the range of the worksheet that isn't the table and use the .clearcontents command.

    Try this: Run the macro recorder, select the range you want cleared and press DELETE. Then stop the macro recorder and look at the code it's written for you.

  3. #3
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: clear contents of entire sheet except the contents of the table

    Hi wayneg,

    There is no magic elixir function for your question. Try code like the following which worked for me:
    Sub KeepTable()
    
      Const sTableRange = "E5:G9"
      
      Dim myRange As Range
      Dim r As Range
      
      'Create the range for the Table to be saved
      Set myRange = Range(sTableRange)
      
      'Process each cell in the Range of the Sheet that is Used
      For Each r In ActiveSheet.UsedRange
      
        'Clear the contents of the cell if the cell is NOT contained in the Table Range
        If Intersect(r, myRange) Is Nothing Then
          r.ClearContents
        End If
      Next r
      
      'Clear Object Pointer
      Set myRange = Nothing
    
    End Sub
    Lewis

  4. #4
    Valued Forum Contributor quekbc's Avatar
    Join Date
    01-18-2010
    Location
    Sydney, Australia
    MS-Off Ver
    2010, 2013, 2016
    Posts
    1,149

    Re: clear contents of entire sheet except the contents of the table

    Long, but does the trick. Just change the reference in red to reference the table you want to keep.
    Sub ClearOutsideTable()
    
    Dim rngKeep As Range, rngThrow As Range
    Dim rng As Range, rngLastCell As Range
        
    
        Set rngKeep = Range("B5:D10")
    
        Set rngLastCell = Cells.SpecialCells(xlCellTypeLastCell)
        
        If rngLastCell.Row < rngKeep.Item(rngKeep.Count).Row Then Set rngLastCell = Cells(rngKeep.Item(rngKeep.Count).Row, rngLastCell.Column)
        If rngLastCell.Column < rngKeep.Item(rngKeep.Count).Column Then Set rngLastCell = Cells(rngLastCell.Row, rngKeep.Item(rngKeep.Count).Column)
        
        If Not rngKeep.Row = 1 Then Set rngThrow = Range("1:" & CStr(rngKeep.Row - 1)).EntireRow
                
        If Not rngLastCell.Row = rngKeep.Item(rngKeep.Count).Row Then
            If rngThrow Is Nothing Then
                Set rngThrow = Range(Cells(rngKeep.Item(rngKeep.Count).Row + 1, 1), Cells(rngLastCell.Row, 1)).EntireRow
            Else
                Set rngThrow = Union(rngThrow, Range(Cells(rngKeep.Item(rngKeep.Count).Row + 1, 1), Cells(rngLastCell.Row, 1)).EntireRow)
            End If
        End If
        
        If Not rngKeep.Column = 1 Then
            If rngThrow Is Nothing Then
                Set rngThrow = Range(Cells(1, 1), Cells(1, rngKeep.Column)).EntireColumn
            Else
                Set rngThrow = Union(rngThrow, Range(Cells(1, 1), Cells(1, rngKeep.Column - 1)).EntireColumn)
            End If
        End If
        
        If Not rngLastCell.Column = rngKeep.Item(rngKeep.Count).Column Then
            If rngThrow Is Nothing Then
                Set rngThrow = Range(Cells(1, rngKeep.Item(rngKeep.Count).Column + 1), Cells(1, rngLastCell.Column)).EntireColumn
            Else
                Set rngThrow = Union(rngThrow, Range(Cells(1, rngKeep.Item(rngKeep.Count).Column + 1), Cells(1, rngLastCell.Column)).EntireColumn)
            End If
        End If
        
        rngThrow.ClearContents
        
    End Sub

  5. #5
    Valued Forum Contributor
    Join Date
    03-24-2014
    Location
    England
    MS-Off Ver
    Excel 2003 - 2016
    Posts
    575

    Re: clear contents of entire sheet except the contents of the table

    Blimey, I'm much lazier than you two. I'd just select the 4 blocks around the table and clear them, all done in 4 lines of code. eg.

    columns("A:C").clearcontents
    columns("F:ZZ").clearcontents
    rows("1:5").clearcontents
    rows("10:1048000").clearcontents

  6. #6
    Forum Contributor
    Join Date
    04-29-2012
    Location
    nuneaton, England
    MS-Off Ver
    Office 365
    Posts
    374

    Re: clear contents of entire sheet except the contents of the table

    whilst away, i looked into moving the table to another sheet, clearing the contents then moving the table back. the issue i have is that the table size isnt fixed so trying to have a set range doesnt work
    Last edited by wayneg; 08-06-2015 at 08:45 AM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Clear contents based on contents of another cell
    By Katrina DTE in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 04-13-2013, 02:44 PM
  2. [SOLVED] Clear contents in another sheet
    By Oeysbrei in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 05-12-2012, 05:20 PM
  3. Excel 2007 : Clear contents on all protected sheet
    By SEHRA in forum Excel General
    Replies: 0
    Last Post: 06-22-2011, 08:36 AM
  4. Clear row contents and copy row contents.
    By fatalcore in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 06-19-2011, 01:08 PM
  5. Copy list contents down, then clear contents
    By hektisk in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-07-2011, 04:34 PM
  6. Table of contents for contents of 1 sheet
    By dhbyrne in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-15-2007, 06:39 PM
  7. Clear sheet contents but retain Clipboard contents
    By nebb in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-25-2007, 01:42 PM
  8. Clear contents of unprotected cells in entire workbook with a macr
    By FinnGirl in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-29-2005, 01:05 PM

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