Delete Any data not on print area

Prani

New Member
Joined
May 29, 2014
Messages
2
Hi

Can anyone recommend any macros for deleting any data not on the print area.

Thanks so much :)
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
There is probably a more efficient way of doing this but this should work.
Code:
Sub caller()
'edit the sheet you want to do this for
Call DeleteOutsideOfPrintArea(Sheets("sheet1"))


End Sub


Sub DeleteOutsideOfPrintArea(WS As Worksheet)
Dim c As Range
For Each c In NonEmptyCells(WS.Cells).Cells
    If Not Intersect(c, WS.Range(WS.PageSetup.PrintArea)) Then
        c.ClearContents
    End If
Next


End Sub






Function NonEmptyCells(TestRange As Range) As Range
    Dim r1 As Range
    Dim r2 As Range
    If Not TestRange.Cells.Count > 1 Then
        Set NonEmptyCells = TestRange
        Exit Function
    End If
    On Error Resume Next
    Set r1 = TestRange.SpecialCells(xlCellTypeFormulas)
    Set r2 = TestRange.SpecialCells(xlCellTypeConstants)
    On Error GoTo 0
    If r1 Is Nothing And Not r2 Is Nothing Then
        Set NonEmptyCells = r2
    ElseIf r2 Is Nothing And Not r1 Is Nothing Then
        Set NonEmptyCells = r1
    ElseIf r2 Is Nothing And r1 Is Nothing Then
        Set NonEmptyCells = TestRange.Cells(1, 1)
    Else
        Set NonEmptyCells = Union(r1, r2)
    End If


End Function
 
Upvote 0
Call it with this code. It loops through all the worksheets and calls the sub.

Code:
'run this to clear all worksheets
Sub WorkbookClearOutsidePrint()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
    DeleteOutsideOfPrintArea ws
Next




End Sub


Sub DeleteOutsideOfPrintArea(ws As Worksheet)
Dim c As Range
For Each c In NonEmptyCells(ws.Cells).Cells
    If Intersect(c, ws.Range(ws.PageSetup.PrintArea)) Is Nothing Then
        c.ClearContents
    End If
Next




End Sub




Function NonEmptyCells(TestRange As Range) As Range
    Dim r1 As Range
    Dim r2 As Range
    Dim dCellCount As Double
    If Int(Application.Version) >= 12 Then
        dCellCount = TestRange.Cells.CountLarge
    Else
        dCellCount = TestRange.Cells.Count
    End If
    If Not TestRange.Cells.CountLarge > 1 Then
        Set NonEmptyCells = TestRange
        Exit Function
    End If
    On Error Resume Next
    Set r1 = TestRange.SpecialCells(xlCellTypeFormulas)
    Set r2 = TestRange.SpecialCells(xlCellTypeConstants)
    On Error GoTo 0
    If r1 Is Nothing And Not r2 Is Nothing Then
        Set NonEmptyCells = r2
    ElseIf r2 Is Nothing And Not r1 Is Nothing Then
        Set NonEmptyCells = r1
    ElseIf r2 Is Nothing And r1 Is Nothing Then
        Set NonEmptyCells = TestRange.Cells(1, 1)
    Else
        Set NonEmptyCells = Union(r1, r2)
    End If




End Function
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,692
Members
448,979
Latest member
DET4492

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top