PFost
PFost

Reputation: 61

Create a VBA Message Box Based on Cell Value

So, this should be fairly easy to implement, but I am unable to get the message box to display.

Context - this is for an estimation tool - if the user enters a value for a particular cell that is less than a minimum threshold, I want to bring up a message box to let them know that's not a valid entry.

The cell I am referring to is named (let's just say TEST). Its location is on the UI tab (1 of 4 tabs in the workbook) is row 47, column 17.

Here is the code I have tried so far:

Sub UI Alerts()
    If Cells(47, 17) < "1000" Then
        MsgBox ("Minimum Value is 1000")
        Exit Sub
    End If

Similarly, with the named cell

Sub UI Alerts()
    If ("TEST").value < "1000" Then
        MsgBox ("Minimum Value is 1000")
        Exit Sub
    End If

I have set this up in the VBA module that corresponds to the UI tab, so I shouldn't have to reference the sheet. Any idea why the message box isn't displaying? Is it possible to automatically display a message box based on a cell value?

Thanks.

Upvotes: 1

Views: 13345

Answers (3)

Robert Todar
Robert Todar

Reputation: 2145

You need to use the Change event within the worksheet. Below is a sample code that should work with your named range. At any rate, it should show you how to do so.

Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address = Range("TEST").Address Then
        If Range("TEST").Value < 1000 Then
            MsgBox ("Minimum Value is 1000")
        End If
    End If
End Sub

Upvotes: 2

Bryan Reis
Bryan Reis

Reputation: 31

Unless there is a specific reason we need this to be a macro, it sounds like standard data validation would work for you here. Try this: select the cell you want the rules applied to, then on the ribbon/toolbar find Data > Data Validation. You can require a whole number greater than 1,000 (or a value in another cell), display a comment box with instructions when the user selects the cell, and an error message if an invalid number is entered.

Upvotes: 2

Terry Carmen
Terry Carmen

Reputation: 3896

You need to add your code to the Worksheet "Change" event.

Right now, you have code but it never gets called.

Upvotes: -1

Related Questions