多个数据validation标准

我有以下Excel表格:

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

用户应该在B列中给每个产品定价。价格是根据列C中的值限制的。在数据validation菜单中,我使用了“Decimal”标准,并将> = C2应用于B列中的每个值。限制工作正常。 但是,稍后用户将看不到列C,因此我还想包括一个小窗口,它显示C列中的值作为用户hover在B列单元格上的build议。

你们有什么想法,如果这是从Excel的数据validation菜单possbile或有macros可以做到这一点?

谢谢你的帮助。

据我所知,有两个选项可以在一个小窗口中显示一个值:

(1)您使用@Robinbuild议的Worksheet_ChangeWorksheet_SelectionChange事件。 然而,这个解决scheme有几个不同的“子选项”:

  1. 您可以使用其他答案中提出的comments
  2. 您可以创build一个小的自定义UserForm来显示您想要显示的任何信息。 这种变化的好处是,你可以自定义表单到你的喜好,并显示你想要的东西。 下面显示了一个可以实现的小样本。 请注意,窗体自动出现,消失,并用光标调整其位置。

在这里输入图像说明

(2)您可以使用Data Validation原来在您的文章中要求。 数据validation允许您不仅限制您希望允许的值。 但是,您也可以指定input消息并自定义错误消息(如果input的值不正确)。 下面的图片给你一个这个解决scheme的视觉想法。

在这里输入图像说明

以下代码片段可以帮助您自动设置所有产品的所有价格validation公式。

 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 

据我所知,Data Validation菜单中没有这个选项。

但是,您可以使用RangeAddComment方法在Price列中的值更改时完成此操作。 您可以使用Worksheet_Change事件来处理更改,然后应用注释:

 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 

效果如下所示:

在这里输入图像说明