Excel VBA - Checking Cell Value in Relation to Another Cell
I want to highlight a row of cells in columns B-K if that part has less than 5 QOH (Column J). I also want it to only highlight the row if the part is in the subinventory "W1" or "OUTSIDE" (Column H).
This is the code I have so far:
'Highlights row if quantity on hand is 5 or less
Dim LastRow As Long, c As Range
Dim MyRange As Range
LastRow = Cells(Cells.Rows.Count, "J").End(xlUp).Row
Set MyRange = Range("J1:J" & LastRow)
With ActiveWorkbook.Worksheets("Sheet1")
For Each c In MyRange
If c <= 5 Then
c.Select
If ActiveCell.Offset(0, -2).Value = "W1" Or "OUTSIDE" Then 'Checks if part is in a specified subinventory
Range("B" & ActiveCell.Row & ":K" & ActiveCell.Row).Interior.Color = vbYellow 'Highlists the row from columns B-K if QOH is less than 5
End If
End If
Next
End With
I have gotten it to highlight rows B-K if the QOH is less than 5. However, the trouble comes when I try to only have it highlight the row if the subinventory is "W1" or "OUTSIDE". When I run the macro, I end up getting error code "Run-time error '1004': Application-defined or object-defined error."
Solution 1:
Try:
If ActiveCell.Offset(0, -2).Value = "W1" Or ActiveCell.Offset(0, -2).Value = "OUTSIDE"
Haven't done VBA in forever and do not currently have it but:
ActiveCell.Offset(0, -2).Value = "W1" Or "OUTSIDE"
does not look correct to me, as '=' has higher precendence than the logical operator 'Or', so this is doing :
(ActiveCell.Offset(0, -2).Value = "W1") Or "OUTSIDE"
The "Or" operator expects two boolean values, and that's where I think your error is coming from (you have a boolean and a string).