Excel Looping Macro

Ryanblades

New Member
Joined
Jan 17, 2018
Messages
2
Hello all,

I am in need of looping the macro code below

Sub Test()
If Range("D2") = "Secondary" Then Range("J2,I2,R2,S2,T2").ClearContents
End Sub

The code above works great for the one line it is clearing but..

I need it to test conditions from 2 to 6000 on each column
If Range("D2") = "Secondary" Then Range("J2,I2,R2,S2,T2").ClearContents
...
...
If Range("D6000") = "Secondary" Then Range("J6000,I6000,R6000,S6000,T6000").ClearContents

Any way to loop this without having to hand type every line?

Thank you for any help
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Try this.
Code:
Sub Test()
Dim I As Long
    
    For I = 2 To 6000
        If Range("D" & I).Value = "Secondary" Then Intersect(Range("I:J,R:T"), Rows(I)).ClearContents
    Next I

End Sub
 
Last edited:
Upvote 0
Norie's is much more succinct.

I like the use of Intersect there...

This was my effort:

Code:
Sub Test()
    Dim c As Range
    
    For Each c In Range("D2:D6000")
        If c = "Secondary" Then Range("J" & c.Row & ",I" & c.Row & ",R" & c.Row & ",S" & c.Row & ",T" & c.Row).ClearContents
    Next c
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,322
Members
452,635
Latest member
laura12345

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