What is wrong with this For Loop code?

thardin

Board Regular
Joined
Sep 29, 2021
Messages
137
Office Version
  1. 365
Platform
  1. Windows
I am trying to loop through each cell in Column U from U4 to last row to check if the percentage in the cell is greater than -.050%. In a previous macro, the format of the cells in column U is set to "0.000%." This Macro isn't working correctly.
Is it because I set the Max variable in the wrong format?
Thanks


Sub CheckII()

Dim ws As Worksheet
Dim Cell As Range
Dim lr As Long

Dim iViolations As Integer
Dim Max As Double
Max = -0.05 / 100

iViolations = 0

For Each ws In ActiveWorkbook.Worksheets
With ActiveSheet
lr = Cells(Rows.Count, "B").End(xlUp).row
For Each Cell In ws.Range("U4:U" & lr)
If Cell.Value > Max Then
'Cell.EntireRow.Interior.ColorIndex = 3
iViolations = iViolations + 1
End If
Next Cell
End With
Next ws

MsgBox iViolations


End Sub
 
Who suggested that?

I said to take the first Then out of the line that I edited, no mention of removing anything else. With that simple change the code should have worked, but cleaning up the rest of the mess and correcting the error to try and avoid several hours of replies bouncing back and forth.
VBA Code:
Sub CheckII()
Dim ws As Worksheet, c As Range, lr As Long
Dim iViolations As Long, iMax As Double
iMax = -0.05 / 100

For Each ws In ActiveWorkbook.Worksheets
    With ws
        lr = .Cells(.Rows.Count, "B").End(xlUp).row
        For Each c In .Range("U4:U" & lr)
            If c.Value > Max And IsNumeric(c.Value) Then
                c.EntireRow.Interior.ColorIndex = 3
                iViolations = iViolations + 1
            End If
        Next Cell
    End With
Next ws

    MsgBox iViolations
End Sub
 
Upvote 0

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
ah, i had to do:

If Cell.Value And IsNumeric(Cell.Value) > Max Then

and that seemed to work
 
Upvote 0
It's still not working actually. That just prevented the empty rows from getting highlighted. Now when I tested it with a row having 0%, it says 0 violations when there's supposed to be 1.
 
Upvote 0
ah, i had to do:

If Cell.Value And IsNumeric(Cell.Value) > Max Then

and that seemed to work
That is what I said to do in post 8.

See the edit to post 11 for revised code, one of your problems was that LR was not looking at the correct sheet.

Also, Max is a reserved vba keyword and as such, should not be used as a variable name. Cell is not reserved but I've changed it anyway to avoid any mix ups between Cell and Cells.
 
Upvote 0
1638378814218.png
 
Upvote 0
Change Cell to c
Also, Change Max to iMax in line 11
 
Upvote 0
It's almost working. Now, in the situation if There is no data under in Row 4 and below It highlights the header rows 1-4

1638379568351.png
 
Upvote 0
That's because your code never allowed for an empty sheet. Think this should tare care of it. You might find that it still highlights row 4 on an empty sheet. On the assumption that there will always be more than 1 row of data in the sheet, you could change If lr >=4 Then to If lr >4 Then which means there must be at least 2 rows of data for it to run on that sheet.
VBA Code:
Sub CheckII()
Dim ws As Worksheet, c As Range, lr As Long
Dim iViolations As Long, iMax As Double
iMax = -0.05 / 100

For Each ws In ActiveWorkbook.Worksheets
    With ws
        lr = .Cells(.Rows.Count, "B").End(xlUp).row
        If lr >=4 Then
            For Each c In .Range("U4:U" & lr)
                If c.Value > iMax And IsNumeric(c.Value) Then
                    c.EntireRow.Interior.ColorIndex = 3
                    iViolations = iViolations + 1
                End If
            Next c
        End If
    End With
Next ws

    MsgBox iViolations
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,214,979
Messages
6,122,550
Members
449,088
Latest member
davidcom

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