VBA to check each row in a column is within a preferred range?

thardin

Board Regular
Joined
Sep 29, 2021
Messages
137
Office Version
  1. 365
Platform
  1. Windows
This is an example of a report I work with.
How would you write a "For each row" statement to make sure the ID's percentage is within preferred range based on the data in column E and V and the limits in the table below.
And if everything is in range: MSbox "no violations"

Note: the amount of rows change on a daily basis.

Thanks

1636724197259.png
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Hi Thardin,

I will suggest inserting a column after column "V" (or any empty column) & in "W1"put formula
Excel Formula:
=IF(VLOOKUP(E1,$D$14:$E$16,2,0)*100<V1,"OVER","OK")
& copy down. This formula will show the correct answer any time & you can filter "OVER" to see in which rows you have violations.
 
Upvote 0
You might consider this vba approach...

VBA Code:
Sub ArrayLoop()
Dim arr As Variant
Dim i As Long
Dim Violations As String

arr = ActiveSheet.UsedRange
For i = 2 To UBound(arr)
    If arr(i, 5) < arr(i, 22) Then Violations = Violations & vbCrLf & arr(i, 4)
Next i
If Violations = "" Then
    MsgBox "No violations"
Else
    MsgBox "Please note the following violations: " & vbCrLf & Violations
End If
End Sub

Cheers,

Tony
 
Upvote 0
Hi Thardin,

I will suggest inserting a column after column "V" (or any empty column) & in "W1"put formula
Excel Formula:
=IF(VLOOKUP(E1,$D$14:$E$16,2,0)*100<V1,"OVER","OK")
& copy down. This formula will show the correct answer any time & you can filter "OVER" to see in which rows you have violations.
I can't do this because I can't change the template of the Worksheet
 
Upvote 0
How about using the last empty column for formulas, after finding out violations you can delete that column?
 
Upvote 0
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.
I used this code because the ID table on the bottom usually isn't there. I included that table so it can be in the picture and thought it would make it easier to understand what I was doing.
This code checks if each ID's Load Percent is within the maximum allowable range, using the data in E5 and V5 in the dataset.



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(in this picture, range A5:AB7)?-

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.
It doesn't work because when there's multiple violations, it doesn't highlight them all, only the 1st one. Also, when there are no violations, a Message box pops up and I have to click "Ok" multiple times depending on the amount of rows.

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
 
Upvote 0

Forum statistics

Threads
1,215,396
Messages
6,124,685
Members
449,179
Latest member
kfhw720

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