Macro Delete All Rows Except for certain cells

jaime1182

New Member
Joined
Dec 11, 2007
Messages
49
Office Version
  1. 2013
Platform
  1. Windows
Dear all

I was hoping for some help with deleting certain columns

My current macro is this:
Code:
Rows(3 & ":" & Rows.Count).ClearContents
Range("D2").Select

This deletes everything from Row 3 onwards. However, I have fixed data I want to keep in A3:B9

Is there a way to modify it so that I can delete only from Cell C3 : (variable column)(last row) thereby essentially keeping A3:B9?

Many thanks in advance!
 
Last edited:

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
.
Code:
Option Explicit




Sub clrContentsExcept()


    Dim Rng As Range
    Dim rCell As Range
    Dim Addr As String
    Dim upDte As Boolean
    On Error Resume Next
    Addr = Application.ActiveWindow.RangeSelection.Address
    Set Rng = ActiveSheet.Range("A3:B9")
    If Rng Is Nothing Then Exit Sub
    upDte = Application.ScreenUpdating
    Application.ScreenUpdating = False
    For Each rCell In ActiveSheet.UsedRange
        If Intersect(rCell, Rng) Is Nothing Then
            rCell.Clear
        End If
    Next
    Application.ScreenUpdating = upDte
End Sub
 
Upvote 0
Logit, that worked perfectly!

Is there a way to keep rows 1 and 2?

Ie:

Code:
 Set Rng = ActiveSheet.Range("A3:B9") & rows(2:3)

Would that work?
 
Last edited:
Upvote 0
This will leave everything above row 3 and A3:B9 intact, while clearing the used range from C3 on.
Code:
Sub ClearAllExcept()
Dim lR As Long, lC As Long
lR = Cells.Find("*", , xlFormulas, xlWhole, xlByRows, xlPrevious).Row
lC = Cells.Find("*", , xlFormulas, xlWhole, xlByColumns, xlPrevious).Column
Range("C3", Cells(lR, lC)).ClearContents
Range("D2").Select
End Sub
 
Upvote 0
Wow - everyone is so quick and fast today

Imma need caffeine to wake up!
 
Upvote 0
.
In my macro you can change this line :

Code:
[COLOR=#333333]Set Rng = ActiveSheet.Range("A3:B9")[/COLOR]

to this line :

Code:
Set Rng = ActiveSheet.Range("A1:B9")
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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