如果该部件的 QOH 小于 5(J 列),我想突出显示 BK 列中的一行单元格。我还希望它只在部件位于子库存“W1”或“OUTSIDE”(H 列)中时突出显示该行。
这是我目前拥有的代码:
'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
如果 QOH 小于 5,我已经让它突出显示行 BK。但是,当我尝试仅让它突出显示子库存为“W1”或“OUTSIDE”的行时,问题就来了。当我运行宏时,我最终收到错误代码“运行时错误‘1004’:应用程序定义或对象定义错误。”
答案1
尝试:
If ActiveCell.Offset(0, -2).Value = "W1" Or ActiveCell.Offset(0, -2).Value = "OUTSIDE"
很久没有用过 VBA 了,目前也没有,但是:
ActiveCell.Offset(0, -2).Value = "W1" Or "OUTSIDE"
在我看来不正确,因为 '=' 的优先级高于逻辑运算符 'Or',因此这样做:
(ActiveCell.Offset(0, -2).Value = "W1") Or "OUTSIDE"
“或”运算符需要两个布尔值,我认为您的错误就是从这里来的(您有一个布尔值和一个字符串)。
答案2
经过反复尝试,我终于明白了。
'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) = "W1" Or ActiveCell.Offset(0, -2) = "OUTSIDE" And ActiveCell.Offset(0, -1) <> "MAJOR COMPONENT" 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