Search code examples
vbarangecomments

If range has specific comment, show msg in another cell


I have this code in VBA and require it to show 'Final Bottling' in another sheet. Below is the code

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

The message appears if all comments in the range C9:H9 have the comment 'Final bottling'. But if only one of the cells in that range has the comment it wont appear anymore. Not sure what to do now, apologies if this sounds dumb and must be an easy fix


Solution

  • This will show the message if any of the cell in C9:H9 in Ip has "Final bottling"

    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