如果范围有特定注释,则在另一个单元格中显示消息

If range has specific comment, show msg in another cell

我在 VBA 中有此代码,并要求它在另一个 sheet 中显示 'Final Bottling'。下面是代码

Ip = input worksheet
op1 = Checks worksheet
i = 1
Cell = Ip.Cells(9, i + 2)
    If LCase(Left(Cell, 14)) = "final bottling" Then
                                                                                                '#Checks Final Bottling
            Op1.Cells(8, 5) = "Final Bottling Run, Please Consume materials. If unsure, check with materials planner!"
        
        Else
        
            Op1.Cells(8, 5) = ""

    End If 'Check

如果 C9:H9 范围内的所有评论都包含评论 'Final bottling',则会出现该消息。但是,如果该范围内只有一个单元格有评论,它就不会再出现了。 不知道现在该怎么做,如果这听起来很愚蠢并且必须是一个简单的解决方案,我们深表歉意

如果 Ip 中 C9:H9 中的任何单元格具有“最终装瓶”

,这将显示消息
Sub Test()
    Dim i As Long
    Dim checkCell As Range
    
    Set checkCell = op1.Cells(8, 5)
    
    For i = 3 To 8
        Debug.Print LCase(Left$(Ip.Cells(9, i).Value, 14))
        If LCase(Left$(Ip.Cells(9, i).Value, 14)) = "final bottling" Then
            checkCell.Value = "Final Bottling Run, Please Consume materials. If unsure, check with materials planner!"
            Exit Sub 'Check is complete, exit sub from here
        End If
    Next i
    
    checkCell.Value = vbNullString 'code will only pass here if it fails all the check in the loop above.
End Sub

您可以使用 Match 函数来检查范围是否包含字符串并设置消息:

pos = 0
On Error Resume Next
pos = Application.WorksheetFunction.Match("final bottling", Ip.Range("C9:H9"), 0)
' if not match (case insensitive), then error occurs and pos remains 0; if match was successful, pos = 1+
Op1.Cells(8, 5) = IIf(pos = 0, "", "Final Bottling Run, Please Consume materials. If unsure, check with materials planner!")
On Error GoTo 0 

另一种方法是在单元格 Opt!E8 中使用这样的公式:

=IFERROR(IF(MATCH("final bottling",Ip!C9:H9,0)>0,"Final Bottling Run, Please Consume materials. If unsure, check with materials planner!",""),"")

或使用Ip.Range("C9:H9").Find()函数