单元格上的Excel VBA数据validation已更改

我有一个名为Employees的工作表,其中有一个名为Department的列。 我也有一个名为“部门”的工作表,其中列AI中有所有可用于员工的部门。 我想要validationEmployees工作表的Department列,例如在Employees工作表的Department列中编写部门时,如果Department中的Department部门工作表中的字体颜色保持不变(黑色),否则字体颜色将更改为蓝色。

这是我到现在为止,但我不能与变化事件单元格pipe理。

Private Sub CommandButton1_Click() Dim i As Integer Dim j As Integer Dim aux As Integer Dim count As Integer For i = 2 To 301 For j = 1 To 4 If Sheet13.Range("H" & i).Value = Sheet14.Range("A" & j) Then aux = 1 Exit For Else Sheet13.Range("H" & i).Font.ThemeColor = 5 End If Next If aux = 1 Then Sheet13.Range("H" & i).Font.ThemeColor = 2 aux = 0 End If Next End Sub 

在这里寻找on cell change事件:

http://msdn.microsoft.com/en-us/library/office/ff839775(v=office.15).aspx

尝试使用这样的东西:

 Private Sub Worksheet_Change(ByVal Target As Range) dim rng as Range set rng = Sheets(1).cells(1,1) ' Change this equal to the range you want to monitor If Not Intersect(Target, rng) Is Nothing Then ' Place the code you want to run here End If End Sub 

记住把它放在工作表的代码(不是在一个单独的模块)

我做了一些研究,并设法validation数据。 这是我的代码:

 Dim o As Integer Dim p As Integer Dim aux As Integer Dim count14 As Integer Dim count13 As Integer Dim KeyCells As Range Set KeyCells = Range("A1:M301") If Not Application.Intersect(KeyCells, Range(Target.Address)) _ Is Nothing Then count13 = Sheet13.Cells(Rows.count, 8).End(xlUp).Row For o = 0 To count13 - 1 count14 = Sheet14.Cells(Rows.count, 1).End(xlUp).Row For p = 1 To count14 If Sheet13.Range("H" & (o + 2)).Value = Sheet14.Range("A" & p) Then aux = 1 Exit For Else Sheet13.Range("H" & (o + 2)).Font.ThemeColor = 6 End If Next If aux = 1 Then Sheet13.Range("H" & (o + 2)).Font.ThemeColor = 2 aux = 0 End If Next End If 

希望这是有帮助的。