How do I apply this VBA code to loop through every row in Used Range??

Status
Not open for further replies.

thardin

Board Regular
Joined
Sep 29, 2021
Messages
137
Office Version
  1. 365
Platform
  1. Windows
I have created this code that successfully performs the task that I want. However, I got it to work only for the first row, "E5" just to see if it worked.
This code checks if each ID is within the maximum allowable range, using the data in columns E5 and V5 in the dataset.


1636836644454.png


Sub TestII()
Dim Sheet As Worksheet
Dim cell As Range
Dim lr As Long
Dim i As Integer


If (Range("E5") = "462266446" And Range("V5") < 3.75) Then
MsgBox "Good"

ElseIf (Range("E5") = "89008P588" And Range("V5") < 3.75) Then
MsgBox "Good"

ElseIf (Range("E5") = "89008P638" And Range("V5") < 4.5) Then
MsgBox "Good"
Else
MsgBox "Bad"
End If
End Sub
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
How do I do this through the entire used range though?

I attempted to use the code below to perform the task through each row, but to highlight the violations instead of message boxes, but it doesn't quite work.
What should I change in order to perform a successful loop?

Thanks

---------------------------------------------------------------------------------------------------------------------------------------------------------------------

Sub Test()
Dim lr As Long
Dim WS As Worksheet
Dim r As Long


lr = Cells(Rows.Count, "C").End(xlUp).Row

For r = 5 To lr

If (Columns(5).Cells(r).Value = "462266446" And Columns(22).Cells(r).Value > 3.75) Then
Rows(5).Interior.ColorIndex = 3

ElseIf (Columns(5).Cells(r).Value = "89008P588" And Columns(22).Cells(r).Value > 3.75) Then
Rows(5).Interior.ColorIndex = 3

ElseIf (Columns(5).Cells(r).Value = "89008P638" And Columns(22).Cells(r).Value > 4.5) Then
Rows(5).Interior.ColorIndex = 3
Else
MsgBox "No Violations"
End If
Next r

End Sub
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Status
Not open for further replies.

Forum statistics

Threads
1,214,649
Messages
6,120,731
Members
448,987
Latest member
marion_davis

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