Help with a For Loop

andrewb90

Well-known Member
Joined
Dec 16, 2009
Messages
1,077
Hello All,

I'm struggling with getting this right, so hopefully somebody out there can assist me.

I am trying to take the range E89:R106 And with each row, If the Value in Column A of that row is "TRUE", nothing happens, otherwise If FALSE, E:R in that row will have the contents cleared.

Thanks!

Andrew
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Is this what you need?

Code:
Sub ClrRng()
Dim rng2clr As Range
For i = 89 To 106 Step 1
    If Not Cells(i, 1) Then
        If rng2clr Is Nothing Then
            Set rng2clr = Range("E" & i & ":R" & i)
        Else
            Set rng2clr = Union(rng2clr, Range("E" & i & ":R" & i))
        End If
    End If
Next i
rng2clr.ClearContents
End Sub
 
Upvote 0
Hi Andrew,

Try this (though initially on a copy of your data as the results cannot be undone if they're not as expected):

Code:
Option Explicit
Sub Macro1()

    Dim lngMyRow As Long
    
    Application.ScreenUpdating = False
    
    For lngMyRow = 89 To 106
        If StrConv(CStr((Range("A" & lngMyRow))), vbUpperCase) = "FALSE" Then
            Range("A" & lngMyRow & ":R" & lngMyRow).ClearContents
        End If
    Next lngMyRow
    
    Application.ScreenUpdating = True

End Sub

Regards,

Robert
 
Upvote 0
Hi andrewb90,

Perhaps you could try this code:

Code:
Sub LoopingTest()

Dim x As Integer

For x = 89 To 106 'assuming that this is the range you need your macro to run at..

If Cells(x, 1) = "False" Then Range("E" & x & ":R" & x) = ""

Next x

End Sub

Good luck!
 
Upvote 0
Thank you Tetra, that works really well. One question, Is it possible to have one or two rows excluded from this? Let's say that I extended the range to row 200, but I didn't want rows 120,130, or 140 to be cleared. I could have this code repeated for each range, but i was just wondering if there was perhaps a more elegant way to go about this?
 
Last edited:
Upvote 0
Here it is:

Code:
Sub ClrRng()
Dim rng2clr As Range
For i = 89 To 200 Step 1
    If Not (Cells(i, 1) Or (i = 120) Or (i = 130) Or (i = 140)) Then
        If rng2clr Is Nothing Then
            Set rng2clr = Range("E" & i & ":R" & i)
        Else
            Set rng2clr = Union(rng2clr, Range("E" & i & ":R" & i))
        End If
    End If
Next i
rng2clr.ClearContents
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,336
Messages
6,124,334
Members
449,155
Latest member
ravioli44

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