How To Check Cell Value In VBA And Display Message Box


This post is to help answer a question from the youtube video at https://www.youtube.com/watch?v=kamx7NnsCZI&t=2s

hi, i need help with the msgbox. I would like a msgbox to pop up if a particular value is smaller than a condition value eg. is 39 < 40, a msgbox should pop up to indicate that the number is smaller. How can I go about doing this?

For starters make sure your workbook is a “.xlsm”

Here is the data:

We are going to loop through the cells in A2 to A14, and check if they are less than the value in D2, and if the value is less, we will display a message box.

Here is the code:

Sub Button1_Click()
    Dim dblValue As Double
    Dim rngValues As Range
    
    'this will be my check cell value
    dblValue = Range("D2")
    
    'Set the range to loop through:
    Set rngValues = Sheet1.Range("A2:A14")
    
    
    'loop through all the cells in the range:
    For Each cell In rngValues
        If cell.Value < dblValue Then
            MsgBox "The value in " & cell.Address & " is smaller than the checked value"
        End If
    Next
    
End Sub

Now when the button “Button 1” is clicked and cells will be evaluated and display a message box if the condition is met.

Let me know if you have any questions.

****************************************************