I have a lot of data (~350 rows) that need to be evaluated against two rows. Imagine the first row is a header, second row and third row are my spec limits. I am basically trying to evaluate whether each of the 350 row items fall within the values specified in Row 2 and Row 3. I will eventually need to apply this across 300 columns as well, where each column is just looking up at the first two rows. I have managed to do this successfully using conditional formatting, but for further processing it would be convenient to have a VBA program that I can manipulate.
I am not a programmer, but after quite a bit of reading and looking at other examples, I have written up something.
When I run this code the entire column becomes red. I added one more line of code to check if it is just a coloring issue by entering 1 when the condition becomes true. Sure enough the whole column populates with 1. It doesn't matter what values I have in AH2 and AH3, the entire column becomes the same color. Interesting thing is, if I replace AH2 and AH3 with hard coded values like say 0 and 1, the code works correctly both for coloring and the column 319. I even tried AH$2 but it doesn't make a difference.
As I mentioned before I need this applied to ~300 columns eventually, so will likely need to add another loop or rewrite this segment of code. But I cannot figure this out.
This looked similar to another issue in this forum; but upon closer look that requirement is different
Really appreciate any help here.
Thank you all so much,
I am not a programmer, but after quite a bit of reading and looking at other examples, I have written up something.
VBA Code:
Sub sorting()
'
Dim i As Long
Dim iCell As Range
For i = 4 To 355
Set iCell = Range("AH" & i)
If iCell.Value > "AH2" Or iCell.Value < "AH3" Then
iCell.Interior.Color = VBA.ColorConstants.vbRed
Cells(i, 319) = 1 'enter 1 in column LG 'added to debug code
End If
Next i
End Sub
When I run this code the entire column becomes red. I added one more line of code to check if it is just a coloring issue by entering 1 when the condition becomes true. Sure enough the whole column populates with 1. It doesn't matter what values I have in AH2 and AH3, the entire column becomes the same color. Interesting thing is, if I replace AH2 and AH3 with hard coded values like say 0 and 1, the code works correctly both for coloring and the column 319. I even tried AH$2 but it doesn't make a difference.
As I mentioned before I need this applied to ~300 columns eventually, so will likely need to add another loop or rewrite this segment of code. But I cannot figure this out.
This looked similar to another issue in this forum; but upon closer look that requirement is different
Coloring a cell based on another cell value using VBA
I usually work on it until I solve it... googling the heck out of it. But it's been days with this one and I can't just figure out what I am doing wrong. I simply need to change the color cell... if the value of an adjacent cell falls between certain range. (I CANT USE Conditional Formatting...
www.mrexcel.com
Really appreciate any help here.
Thank you all so much,