Code Adjustment to clear contents from a range instead of deleting entire rows needed

kelly mort

Well-known Member
Joined
Apr 10, 2017
Messages
2,169
Office Version
  1. 2016
Platform
  1. Windows
This code deletes the entire row. Now I want to clear just data from a given range intead. I want to clear data from T4:X & LastusedRow. V contains the dates. I have been trying to fix it but seems hard to read and understand it. Actually the dates are sorted in descending order.

Code:
Sub DeleteRows()
    Dim c As Range, DeleteRange As Range, DataRange As Range
    Dim LR As Long
    
'change sheet name as required
    With Worksheets("[COLOR=#ff0000]Sheet1[/COLOR]")
'find last row in range
        LR = .Cells(.Rows.Count, "B").End(xlUp).Row
'range you are searching
        Set DataRange = .Range("B1:B" & LR)
    End With


    DataRange.EntireRow.Hidden = False


    For Each c In DataRange.Cells
        If IsDate(c.Value) Then
            If Date - DateValue(c.Value) > 15 Then
                If DeleteRange Is Nothing Then
                    Set DeleteRange = c
                Else
                    Set DeleteRange = Union(DeleteRange, c)
                End If
            End If
        End If
    Next c
'delete all matched rows in one go
    If Not DeleteRange Is Nothing Then DeleteRange.EntireRow.Delete
End Sub
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Try changing this:
Code:
DeleteRange.EntireRow.Delete

to this:
Code:
DeleteRange.ClearContents
 
Upvote 0
Try changing this:
Code:
DeleteRange.EntireRow.Delete

to this:
Code:
DeleteRange.ClearContents

I tried this.

It deleted only from column V not from T to X


Okay this worked:

Code:
DeleteRange.Offset(0, -2).Resize(, 5).ClearContents


Thanks
 
Last edited:
Upvote 0
You're welcome, glad you figured it out.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,178
Members
453,021
Latest member
Justyna P

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