Im not vba savvy so I thought this is the best place to ask. My aim is to have a msgbox appear when a specific cell is not equal to 'X' value. Here is the code I have below
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim myCell As Range
For Each myCell In Range("G4:G160")
If myCell.Value <> 17521 Then
MsgBox "INCORRECT SKU RECHECK PALLET AND INFORM SUPERVISOR"
Exit Sub
End If
Next myCell
End Sub
The cell value is taken from another cell so not manually entered in. When I run this macro it appears everytime i change cells or click. Also appears when the number is correct. I only need this msgbox to appear only when it is not equal to the value.
I would also like to have the message to be in the colour red. If this is easy enough please let me know what to do or change. Biggest learning curve for me as I always avoided vba code.
EDIT: Please look at link below. Barcodes to the right 1,2 and 3 are correct to be scanned into B4 and so on and the wrong ones 4 and 5. Im hoping for msgbox or userform to visually show only the wrong ones. If correct ones are entered then no pop up.
enter image description here https://1drv.ms/x/s!AvRcvV5GEpBXnRIrzT2xDTv0iTQh?e=tV3LeN
Your code seems to be right. Once you select another cell in your target sheet, this code runs and will never show the msgbox if all values in the range "G4:G160" are equal to 17521. If it appears, there is certainly at least one value <>17521.
I recommend to use the Change Event
instead:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim myCell As Range
For Each myCell In Range("G4:G160")
If (Not IsEmpty(myCell)) And myCell.Value <> 17521 And myCell.Value <> "" Then
MsgBox "INCORRECT SKU RECHECK PALLET AND INFORM SUPERVISOR", vbCritical
Exit Sub
End If
Next myCell
End Sub
About the red color of the msgbox, you cannot change the color of a msgbox. You have two options:
Critical
icon as:MsgBox "INCORRECT SKU RECHECK PALLET AND INFORM SUPERVISOR", vbCritical
In this case, you'll see something like this: