Excel:如果列A和B中选定的date不匹配,则触发C列中的特定下拉列表
我有一个电子表格如下:
在P和Q列中,用户可以从下拉列表中selectdate。 如果select的date匹配,我希望列R中的相应单元格表示“N / A”。 如果所选单元格不匹配,我想要一个下拉列表出现在R列的相应单元格中。
列名是瑞典语。
私人小组Worksheet_Change(BYVAL目标作为范围)
Dim LastRow As Integer 'The last row of used range Dim i As Integer 'Used to loop On Error Resume Next LastRow = Cells.Find(What:="*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row 'Finds the last row On Error GoTo 0 If Not Intersect(Target, Range("Q:Q")) Is Nothing Then For i = 2 To LastRow If (Year(Cells(i, 16)) = Year(Cells(i, 17)) And Month(Cells(i, 16)) = Month(Cells(i, 17)) And Day(Cells(i, 16)) = Day(Cells(i, 17))) Then Cells(i, 18) = "N/A" Else Cells(i, 18).Clear With Cells(i, 18).Validation .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _ xlBetween, Formula1:="=Sheet2!$A$1:$A$3" 'Change refrence here .IgnoreBlank = True .InCellDropdown = True .ShowInput = True .ShowError = True End With End If Next i End If
结束小组
嘿乌登,虽然不是有史以来最伟大的代码,这应该有所帮助。 您将需要更改引用到您的下拉菜单。 你也可以使用命名的范围,只是search这个,这并不难。 如果您还有其他问题,请告诉我。 编辑:我只是假设你知道一些VBA,如果你不,按ALT + F11然后双击列表A,并粘贴在我的代码。 在代码中看到我的评论,该代码以“'”开头,找出改变的参考。
这可以通过Data Validation
公式来实现
在R列中,select您想要进行Data Validation
所有范围,然后出现此窗口,
然后在Allow
部分selectList
,你会看到这个
在源代码部分,你会写这个:
=IF($B2<>$C2,$J$11:$J$22,$X$11)
哪里:
$B2
和$C2
是具有我想比较的date的列,因为您将是P
和Q
第一个清单$J$11:$J$22
是如果date不匹配,我想显示的清单范围。
第二列$X$11
,只是一个有“ N/A
”的单元格来显示B2
和C2
的date是否已经匹配。
因为我在IF
公式中的逻辑testing, If B2 and C2 are NOT equal, show the first list, if not (If B2 and C2 are equals) then show the secound list
那么你可以有这个: