0

I have the following Excel table:

      A                   B                C    
1     Products            Price        Minimum Price
2     Product A           $30             $10  
3     Product B           $20             $25
4     Product C           $10             $15

The user should put a price per product in column B. The prices are limited based on the values in column C. In the data validation menu I used the "Decimal" criteria and put >= C2 applying to each value in column B. This restriction works fine. However, later on the user will not see column C therefore I also want to include a small window which shows the value in column C as a suggestion when the user hovers over the cell in column B.

Do you guys have any idea if this is possbile with the data validation menu from Excel or is there a macro which can do this?

Thanks for any help.

Michi
  • 4,663
  • 6
  • 33
  • 83

2 Answers2

2

As far as I know, there's no option in the Data Validation menu for this.

However, you can use the AddComment method of a Range to accomplish this when a value in the Price column is changed. You can use the Worksheet_Change event to handle a change and then apply the comment:

Private Sub Worksheet_Change(ByVal Target As Range)

    Dim strMininmumPrice As String

    'test if change was made in price values
    If Not (Intersect(Target, Sheet3.Range("B2:B4")) Is Nothing) Then
        'add neighbour cell value to message
        strMinimumPrice = "Minimum price was: " & CStr(Target.Offset(0, 1).Value)

        'create and add comment to target cell
        Target.AddComment strMinimumPrice

    End If

End Sub

Effect looks like this:

enter image description here

Robin Mackenzie
  • 18,801
  • 7
  • 38
  • 56
  • Hi Robin, thanks for your answer. It is a nice idea to do it that way but in the Excel spreadsheet I am using you cannot use the Worksheet Change event since the undo function of Excel needs to be working all the time. – Michi Jun 13 '16 at 09:53
  • @Robin: Actually, there is such an option (see my answer). Still, your solution is also a feasible alternative! So, *(+1)* from my side. – Ralph Jun 13 '16 at 19:26
2

As far as I can tell you have two options to show a value in a small window:

(1) You make use the Worksheet_Change or Worksheet_SelectionChange event as suggested by @Robin. Yet, there are several different "sub-option" available with this solution:

  1. You could use the comments as proposed in the other answer or
  2. you could create a small custom UserForm to show any information you wish to show. The good thing with this variation is that you can customize the form to your preferences and show pretty much anything you want. The following shows a small sample of what could be achieved that way. Note, that the form automatically appears, vanishes, and adjusts its position with the cursor.

enter image description here

(2) You could make use of the Data Validation as originally asked for in your post. The data validation allows you not only to limit the values which you would like to allow for. But you can also specify an input message and customize the error message (if an incorrect value is entered). The following picture gives you a visual idea of this solution.

enter image description here

The following code snippet could help you to automatically set all price validation formulas for all products.

Option Explicit

Sub AutomaticallySetDataValidations()

Dim lngRow As Long
Dim strProduct As String
Dim dblMinimumPrice As Double

With ThisWorkbook.Worksheets(1)
    For lngRow = 2 To .Cells(.Rows.Count, "A").End(xlUp).Row
        strProduct = .Cells(lngRow, 1).Value2
        dblMinimumPrice = IIf(IsNumeric(.Cells(lngRow, 3).Value2), CDbl(.Cells(lngRow, 3).Value2), 0)
        If dblMinimumPrice > 0 Then
            With .Cells(lngRow, "B").Validation
                .Delete
                .Add Type:=xlValidateDecimal, AlertStyle:=xlValidAlertStop, Operator _
                    :=xlGreaterEqual, Formula1:=dblMinimumPrice
                .IgnoreBlank = True
                .InCellDropdown = True
                .InputTitle = "Price - " & strProduct
                .ErrorTitle = "Invalid price!"
                .InputMessage = "Please enter a new price for " & strProduct & _
                    ". The minimum admissable price for this product is " & Format(dblMinimumPrice, "$#,##0") & "."
                .ErrorMessage = "The price you entered is not acceptable. Please enter a new value."
                .ShowInput = True
                .ShowError = True
            End With
        Else
            Debug.Print "No data validation set for row " & lngRow & " since there is no valid minimum price."
        End If
    Next lngRow
End With

End Sub
Ralph
  • 9,284
  • 4
  • 32
  • 42
  • Great answer. Nice animations! – Robin Mackenzie Jun 13 '16 at 23:40
  • Hi Ralph, sounds a like a good idea. I will check it out today. – Michi Jun 14 '16 at 06:24
  • Option two looks great but I would have to make an individual "Input Message" including the price from column C for each product (row). For three products that is o.k. but I only used them for posting here. In my original file I will have 1000 products. Nice animations by the way :-) – Michi Jun 14 '16 at 09:50
  • @Michi It seems that you don't know how to use [the Excel macro recorder](https://www.youtube.com/watch?v=Q_HQGHPBYoo) yet. Therefore, I recorded a small VBA code snippet which automatically sets the data validation for all products for you (assuming that the product name is in column A and the minimum price is in column C and that all prices are on sheet 1). Yet, the code can be easily adjusted to whatever suits your needs. – Ralph Jun 14 '16 at 10:17
  • Hi Ralph, I used your code and it works. The only problem is that the minimum prices in column C are not remaining the same. In my original file there is a formula behind it which calculates those prices based on different input values. Once the minimum price changes in column C the validation message is not updated automatically. The only way to do it is probably to connect your code with the worksheet change event. However, when I inlcude the worksheet change event the undo function is not usable anymore. I am not sure if there is a solution for this issue. Thanks for all your effort. – Michi Jun 14 '16 at 12:26
  • @Michi The original question as posted has been answered in full (and even beyond). If you have a new question then I suggest that you post it. If you believe that a reference to this post will be of help to put the new question into context then you can even include a link to this post. – Ralph Jun 14 '16 at 12:39