Clear all contents, formula, objects etc of cells outside a dynamic range

Vincent88

Active Member
Joined
Mar 5, 2021
Messages
382
Office Version
  1. 2019
Platform
  1. Windows
  2. Mobile
Hi Guys,
I found this code said to clear all contents of cells outside a dynamic range. How to modify to suit my need ? My dynamic range is
Range1 = Cells(1, 1).Resize(lastrowinRng, lastcolinRng).

VBA Code:
Sub DeleteAllDataNotInSelection4()

  Dim vArr As Variant, UnusedRow As Long, R1str As String, R1 As Range
  Dim lastrowinRng As Long, lastcolinRng As Long
  Dim Range1 As Range
 
  lastrowinRng = Range("A" & Rows.Count).End(xlUp).Row
  lastcolinRng = Cells(1, Columns.Count).End(xlToLeft).Column
 
  Set Range1 = Cells(1, 1).Resize(lastrowinRng, lastcolinRng)
 
  Debug.Print Range1.Address
 
  Const DefinedName As String = Range1
 
  Set R1 = Range(DefinedName)
  R1str = "=" & R1.Parent.Name & "!" & R1.Address
  UnusedRow = Cells.Find("*", , xlFormulas, , xlRows, xlPrevious).Row + 1
  Application.ScreenUpdating = Falseq
  R1.Copy Cells(UnusedRow, "A")
  Rows("1:" & UnusedRow - 1).Clear
  Cells(UnusedRow, "A").Resize(R1.Rows.Count, R1.Columns.Count).Cut R1
  ActiveSheet.Names.Add DefinedName, R1str
  Application.ScreenUpdating = True

End Sub
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Add Two Last lines of my code:
VBA Code:
Sub DeleteAllDataNotInSelection4()

  Dim vArr As Variant, UnusedRow As Long, R1str As String, R1 As Range
  Dim lastrowinRng As Long, lastcolinRng As Long
  Dim Range1 As Range
 
  lastrowinRng = Range("A" & Rows.Count).End(xlUp).Row
  lastcolinRng = Cells(1, Columns.Count).End(xlToLeft).Column
 
  Set Range1 = Cells(1, 1).Resize(lastrowinRng, lastcolinRng)
  Range(Cells(lastrowinRng + 1, 1), Cells(Rows.Count, Columns.Count)).ClearContents
  Range(Cells(1, lastcolinRng + 1), Cells(Rows.Count, Columns.Count)).ClearContents
End Sub
 
Upvote 0
Or Use these two Lines:
VBA Code:
Rows(lastrowinRng + 1 & ":" & Rows.Count).ClearContents
Columns(Split(Cells(1, lastcolinRng + 1).Address, "$")(1) & ":" & Split(Cells(1, Columns.Count).Address, "$")(1)).ClearContents
 
Upvote 0
Hi maabadi,
Other than values to clear, I add below lines but not work ?

Code:
Columns(Split(Cells(1, lastcolinRng + 1).Address, "$")(1) & ":" & Split(Cells(1, Columns.Count).Address, "$")(1)).OLEObjects.Delete
Columns(Split(Cells(1, lastcolinRng + 1).Address, "$")(1) & ":" & Split(Cells(1, Columns.Count).Address, "$")(1)).Pictures.Delete

VBA Code:
 Rows(lastrowinRng + 1 & ":" & Rows.Count).OLEObjects.Delete
 Rows(lastrowinRng + 1 & ":" & Rows.Count).Pictures.Delete
 
Upvote 0
Try this for shapes:
VBA Code:
Sub DeleteAllDataNotInSelection4()
  Dim lastrowinRng As Long, lastcolinRng As Long, Range1 As Range, shp As Shape
   lastrowinRng = Range("A" & Rows.Count).End(xlUp).Row
  lastcolinRng = Cells(1, Columns.Count).End(xlToLeft).Column
 
  Set Range1 = Cells(1, 1).Resize(lastrowinRng, lastcolinRng)

    For Each shp In ActiveSheet.Shapes
            If Not Intersect(shp.TopLeftCell, Range1) Is Nothing Then
                    shp.Delete
            End If
    Next shp
 
Upvote 0
Try this for shapes:
VBA Code:
Sub DeleteAllDataNotInSelection4()
  Dim lastrowinRng As Long, lastcolinRng As Long, Range1 As Range, shp As Shape
   lastrowinRng = Range("A" & Rows.Count).End(xlUp).Row
  lastcolinRng = Cells(1, Columns.Count).End(xlToLeft).Column
 
  Set Range1 = Cells(1, 1).Resize(lastrowinRng, lastcolinRng)

    For Each shp In ActiveSheet.Shapes
            If Not Intersect(shp.TopLeftCell, Range1) Is Nothing Then
                    shp.Delete
            End If
    Next shp
Hi maabadi,
this does not work.
 
Upvote 0

Forum statistics

Threads
1,214,896
Messages
6,122,132
Members
449,066
Latest member
Andyg666

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