首页 > 解决方案 > 如果范围有特定注释,则在另一个单元格中显示 msg

问题描述

我在 VBA 中有这段代码,并要求它在另一张纸上显示“最终装瓶”。下面是代码

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 范围内的所有评论都有评论“最终装瓶”,则会出现该消息。但是,如果该范围内只有一个单元格具有注释,它将不再出现。不知道现在该怎么做,如果这听起来很愚蠢并且必须是一个简单的解决方案,请道歉

标签: vbarangecomments

解决方案


如果 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

推荐阅读