我有一个名为Employees的工作表,其中有一个名为Department的列。我还有一个名为Departments的工作表,在A列中,我有所有可用于Employees的部门。我想验证Employees工作表的Department列,例如当我在Employees工作表的Department列中写一个部门时,如果部门在Departments工作表中,字体颜色保持不变(黑色),否则字体颜色变为蓝色。 / p>
这是我迄今为止所拥有的,但我无法管理更改事件中的单元格。
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
答案 0 :(得分:0)
答案 1 :(得分:0)
尝试使用以下内容:
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
请记住将其放在工作表代码中(而不是单独的模块中)
答案 2 :(得分:0)
我做了一些研究,并设法验证了数据。这是我的代码:
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
希望它有所帮助。