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

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
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,224,825
Messages
6,181,190
Members
453,020
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