Excel VBA Msg Box when cell meets criteria

Solved/Closed
Report
-
Posts
4476
Registration date
Thursday January 28, 2010
Status
Contributor
Last seen
August 2, 2020
-
Hello,

Hi this will probably be very simple for someome however I am having a nightmare day, all I want is a vba formual which will bring up a message box when a cell is showing a specific criteria.

Can anyone help?

Cheers

Ian

2 replies

This code assumes that what you are looking for is in column A.

Private Sub Find_Criteria()

Dim i
Dim r
r = Range("A65536").End(xlUp).Row
i = 1


For I = I To r

If Range("A" & i) = "criteria" Then

MsgBox "Found" & " " & Range("A" & i).Address

End If

Next i

End Sub

If you are looking for certain criteria in a range of cells, then try something like this.

Private Sub Find_Criteria()

Dim I As Variant
Dim FindRange As Range
Set FindRange = Range("A1:K50")

For Each I In FindRange

If I = "criteria" Then

MsgBox "Found" & " " & i.Address

End If

Next i

End Sub
5
I can not get this code to work, I am getting an ambiguos error I do need all 3 message boxes to come up at the same time if the < value is met. Thanks any help is appreciated

Private Sub Worksheet_Calculate()
If Range("F10").Value < 5300 Then
MsgBox "Initial Load Must be at least 5,300Kgs"

End If
End Sub

Private Sub Worksheet_Calculate()
If Range("G10").Value < 4300 Then
MsgBox "Initial Load Must be at least 4,300Kgs"

End If
End Sub

Private Sub Worksheet_Calculate()
If Range("H10").Value < 1300 Then
MsgBox "Initial Load Must be at least 1,300Kgs"

End If
End Sub
1
Posts
4476
Registration date
Thursday January 28, 2010
Status
Contributor
Last seen
August 2, 2020
768
You have three sub routines with same name and signature It cannot be so. You need to do like this

Private Sub Worksheet_Calculate()

If Range("F10").Value < 5300 Then
MsgBox "Initial Load Must be at least 5,300Kgs"

End If

If Range("G10").Value < 4300 Then
MsgBox "Initial Load Must be at least 4,300Kgs"

End If

If Range("H10").Value < 1300 Then
MsgBox "Initial Load Must be at least 1,300Kgs"

End If
End Sub
0
>
Posts
4476
Registration date
Thursday January 28, 2010
Status
Contributor
Last seen
August 2, 2020

Thank you so much that worked, I have been pounding my head on the wall for days trying to do it my self some times it is just best to ask.

Again Thank you Jerry
0
Posts
4476
Registration date
Thursday January 28, 2010
Status
Contributor
Last seen
August 2, 2020
768 > cx72go
Not know exactly why message boxes are needed. But in case you can live with one message box that have all possible errors, you can try this

Private Sub Worksheet_Calculate()
Dim myMessage As String

    myMessage = ""
    
    If Range("F10").Value < 5300 Then
        If (myMessage <> "") Then myMessage = myMessage & vbCrLf
        myMessage = myMessage & "Initial Load Must be at least 5,300Kgs"

    End If

    If Range("G10").Value < 4300 Then
        If (myMessage <> "") Then myMessage = myMessage & vbCrLf
        myMessage = myMessage & "Initial Load Must be at least 4,300Kgs"

    End If

    If Range("H10").Value < 1300 Then
        If (myMessage <> "") Then myMessage = myMessage & vbCrLf
        myMessage = myMessage & "Initial Load Must be at least 1,300Kgs"

    End If
    
    If (myMessage <> "") Then MsgBox (myMessage)

End Sub
0
>
Posts
4476
Registration date
Thursday January 28, 2010
Status
Contributor
Last seen
August 2, 2020

I am trying to tell a lab person when there is not enough product in a vessel to hit the agitator blades at the same time I am trying to remind him how much he does need. I first tried to make an adjacent cell highlight and appear with the correct value using conditional formatting but I was unable to get that to work so I tried vba.

Thanks jerry
0
Posts
4476
Registration date
Thursday January 28, 2010
Status
Contributor
Last seen
August 2, 2020
768 > cx72go
Jerry I meant there are three messages. So potentially three time ok needs to be clicked. There can also be one message with all the warnings listed. Thats what I tried to show in the macro in message #5. I thought it may be of interest to you. Of course there might be cases, where every warning needs to come up one by one and this might be one of those case.
0