How do I clear content of a dynamic range

fhzhkunming

New Member
Joined
Mar 4, 2021
Messages
30
Office Version
  1. 365
Platform
  1. Windows
Hi Members,

I would like to clear content of a dynamic range based on a condition, i,e., I want to clear content in range "D:I" if value in cells of column "I" is 0. In current situation, I wanted to clear "D14:I17". But I got an error message "Run-time error '1004': Application-defined or object-defined error. Here is my code:
VBA Code:
Dim rowNum As Integer
    Dim Lrow As Long
    With ActiveSheet
    Lrow = Cells(Rows.Count, "I").End(xlUp).Row
        For rowNum = Lrow To 2 Step -1
        If Range("I" & rowNum).Value = 0 Then
            .Range("D:I" & Lrow).ClearContents 
        End If
    Next rowNum
    End With
End Sub

1615306481801.png


Please help me out. Thanks in advance.

Frank
 

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
VBA Code:
.Range("D" & rowNum).Resize( , 6).ClearContents
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,250
Messages
6,123,887
Members
449,130
Latest member
lolasmith

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