这是一个有趣的问题,当在 A 列中输入维度代码时,我使用在 B 列中的单元格上设置验证的方法得到了下面的代码。
一旦选择了一个选项,B 列中文本的颜色就会变为蓝色,但您想要的绿色和红色文本实际上是不可能的,因为在单元格下拉列表中,无论单元格的字体颜色如何,始终显示黑色.
代码并不完美,但更多的是概念证明,可以让您抢占先机。
Dim CHANGING_VAL As Boolean 'Global Variable that can be set to prevent the onchange being fired when the Macro is removing the description from the dropdown.
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
If Target.Column = 2 And CHANGING_VAL = False Then
CHANGING_VAL = True
If InStr(1, Target.Value, "~") > 2 Then
Target.Value = Left(Target.Value, InStr(1, Target.Value, "~") - 2)
End If
Target.Validation.Delete
Target.Font.Color = RGB(0, 0, 255)
CHANGING_VAL = False
End If
End Sub
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
If Target.Column = 2 Then
If Target.Offset(0, -1) <> "" Then
strValidList = ""
For intRow = 1 To 300
If Sheets("Data").Cells(intRow, 1) = Target.Offset(0, -1) Then
If Sheets(Target.Parent.Name).Cells(3, 4) = "English" Then
strValidList = strValidList & Sheets("Data").Cells(intRow, 2) & " ~ " & Sheets("Data").Cells(intRow, 3) & ", "
Else
strValidList = strValidList & Sheets("Data").Cells(intRow, 2) & " ~ " & Sheets("Data").Cells(intRow, 4) & ", "
End If
End If
Next
If strValidList <> "" Then
strValidList = Left(strValidList, Len(strValidList) - 2)
Target.Select
With Selection.Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:=strValidList
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
End With
End If
End If
Else
Sheets(Target.Parent.Name).Range("B:B").Validation.Delete
End If
End Sub