Loop with conditional IF statement

lcwhite

New Member
Joined
Apr 8, 2014
Messages
16
Hey Guys - I'm pretty new to these macros and I can't see to record what I'm trying to do. Here is the scenario: COL K COL L COL AE
0 4 -133
0 1 12
3 0 1
2 0 1.5
0 0 1
0 0 342
IF COL AE is < 1.5 & (K > 0 or L > 0) THEN Highlight COL AE in red(255). I need to Loop thru COL AE as long as there is data in K. I tried to record this with conditional formatting, but just couldn't get it to work. Thanks so much! Lori
 
Loops are valuable in many cases, but in general if you are able to avoid them then you should probably avoid them (they are incredibly inefficient). In this case I am using the native autofilter feature in excel to create only ranges that need to be worked with and affecting those ranges (for the font color). The other scenario you are needing to implement is making a formula change to a range which can be done effectively without looping in general.

As for the error, I went crazy with the cells statements and mixed 2 methods sorry:
Code:
Set Rng = WS.Range(WS.Cells(1, 1), WS.Range("AE" & Cells(Rows.Count, 11).End(xlUp).Row))
 
Upvote 0

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.
Thanks Brian! I'm really learning a lot from you :). You will notice in my If condition I have If (AE < 1.5) AND (K > 0 OR L > 0) THEN Highlight Red. Your code has 2 separate steps, which doesn't give me the result I need.
Rng.AutoFilter Field:=31, Criteria1:="< 1.5", Operator:=xlAnd
Rng.AutoFilter Field:=11, Criteria1:="> 0", Operator:xlOr Rng.AutoFilter Field:=12, Criterial1:=">0"
Set Rng2 = Intersect(Rng, Rng.Offset(1))
Rng2.Columns(31).SpecialCells(xlCellTypeVisible).Font.Color = vbRed Is this what I need to do? Also why can't I format anything on this forum? Seems others can.....
 
Upvote 0
This formula works perfectly populating rows AE2:AE3495 with my results.
Range("AE2:AE3495").Formula = "=IFERROR(+IF(+K2=0,0,+R2/(+IF(+K2>L2,K2,L2)*$AE$1/365)/P2),0)"
However, it seems better logic to stick this formula inside a Loop as I don’t always know the number of rows I will have.
LastRow = Worksheets("Sheet1").Cells(Rows.Count, "K").End(xlUp).Row
For i = 2 To LastRow
ActiveCell.Formula = "=IFERROR(+IF(+K2=0,0,+R2/(+IF(+K2>L2,K2,L2)*$AE$1/365)/P2),0)"
If (Worksheets("Sheet1").Range("AE" & i).Value < 1.5) And _
((Worksheets("Sheet1").Range("K" & i).Value > 0) Or (Worksheets("Sheet1").Range("L" & i).Value > 0)) Then
Worksheets("Sheet1").Range("AE" & i).Font.Color = 255

End If
Next i
My syntax is now wrong, as I’m only populating the first cell with my formula. What am I missing?? Thanks you guys!!! I’m really new to this.

Try This:

Populate as well the Cell number with Loop Variable (If you still prefer to use it).

ActiveCell.Formula = "=IFERROR(+IF(+K" & i & "=0,0,+R" & i & "/(+IF(+K" & i & ">L" & i & ",K" & i & ",L" & i & ")*$AE$1/365)/P" & i & "),0)"

Cheers
Rich
 
Last edited:
Upvote 0
Thanks Brian! I'm really learning a lot from you :). You will notice in my If condition I have If (AE < 1.5) AND (K > 0 OR L > 0) THEN Highlight Red. Your code has 2 separate steps, which doesn't give me the result I need.
Rng.AutoFilter Field:=31, Criteria1:="< 1.5", Operator:=xlAnd
Rng.AutoFilter Field:=11, Criteria1:="> 0", Operator:xlOr Rng.AutoFilter Field:=12, Criterial1:=">0"
Set Rng2 = Intersect(Rng, Rng.Offset(1))
Rng2.Columns(31).SpecialCells(xlCellTypeVisible).Font.Color = vbRed Is this what I need to do? Also why can't I format anything on this forum? Seems others can.....

There is no technical way to in 1 step perform an or on 2 different fields using autofilter (this can be done in advanced filtering), however, using the 2 steps I used has in essence done the same thing as a literal AE<1.5 AND (K>0 OR L>0). Now if you are not getting the expected results then perhaps we need to determine exactly what you are desiring.
Code:
Rng.AutoFilter Field:=31, Criteria1:="<1.5"
Rng.AutoFilter Field:=11, Criteria1:=">0"
Rng2.Columns(31).SpecialCells(xlCellTypeVisible).Font.Color = vbRed
Finds all instances where AE<1.5, and K>0 and changes the font color to red on those found cells in column AE
Code:
Rng.AutoFilter Field:=31, Criteria1:="<1.5"
Rng.AutoFilter Field:=12, Criteria1:=">0"
Rng2.Columns(31).SpecialCells(xlCellTypeVisible).Font.Color = vbRed
Finds all instances where AE< 1.5, and L>0 and changes the font color to red on those found cells in column AE

Doing this in 2 steps or 1 step as far as I understand your needs should not change the results (just have to remember to reset the autofilter between both steps).

If you are having problems formatting your posts you may try using the Go Advanced button that is available under your post. You can use code and html tags to keep alignments, as well as font colors, font styles and things of that nature (for my normal post quick reply option some of the options are available, but it may be a setting; I am not really a good forum user).
 
Upvote 0
Try This:

Populate as well the Cell number with Loop Variable (If you still prefer to use it).

ActiveCell.Formula = "=IFERROR(+IF(+K" & i & "=0,0,+R" & i & "/(+IF(+K" & i & ">L" & i & ",K" & i & ",L" & i & ")*$AE$1/365)/P" & i & "),0)"

Cheers
Rich
You also would have to either update the active cell or use another method to loop the row you are add the formula to, at least I didn't see where the activecell was being updated. For example:
Code:
Sub Macro2()
Dim WB As Workbook, WS As Worksheet, Rng As Range, CE As Range

Set WB = Workbooks("Sample.xlsx")
Set WS = WB.Sheets(1)
Set Rng = WS.Range("AE2", WS.Cells(Rows.Count, 11).End(xlUp).Offset(, 20))
For Each CE In Rng
    CE.Formula = "=IFERROR(IF(K" & CE.row & "=0,0,R" & CE.row & "/(F(K" & CE.row & ">L" & CE.row & ",K" & CE.row & ",L" & CE.row & ")*$AE$1/365)/P" & CE.row & "),0)"
    If CE.Value < 1.5 And (CE.Offset(, -20).Value > 0 Or CE.Offset(, -19).Value > 0) Then CE.Font.Color = 255
Next
End Sub
 
Upvote 0
Rng2.Columns(31).Formula = "=IFERROR(IF(K2=0,0,R2/(IF(K2>L2,K2,L2)*$AE$1/365)/P2),0)"
This is working great for me. Now I want to change the value of the error from 0 to "NO USAGE" and ROUND the output of the formula by 2. Would it look like this: Rng2.Columns(31).Formula = "=IFERROR(ROUND(IF(K2=0,0,R2/(IF(K2>L2,K2,L2)*$AE$1/365)/P2),2),"NO USAGE")"
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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