我在excel表中有两列A和B。列是包含“是”和“否”的下拉列表。我想根据下拉列表中 A 单元格的文本值更改 B 单元格的颜色。例如,如果我在 A1 单元格中选择“是”,那么 B1 单元格应该显示绿色。A2、A3...等
我不是程序员,所以我在 VBA 编码方面真的很菜。对于这种情况,条件形成也有问题。
如果有人对此有答案,那将是我的荣幸。
对您的代码进行了一些更改。
Sub RowFormat()
Dim A As Range
For Each A In Range("A1:A" & Cells(Rows.Count, "A").End(xlUp).Row)
If Not IsError(A) Then
If A.Value = "Yes" Then
A.Offset(0, 1).Interior.ColorIndex = 6
ElseIf A.Value = "No" Then
A.Offset(0, 1).Interior.ColorIndex = 3
Else
A.Offset(0, 1).Interior.ColorIndex = xlNone
End If
End If
Next A
End Sub
使用条件格式。
对于“是”使用=A1="Yes"
,
对于“否”使用=A1="No"
和格式相应地应用格式。
编辑 :
如果您使用Worksheet_Change事件,请使用以下代码。
Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target, Range("A:A")) Is Nothing Then Exit Sub 'check for range
If Target.Value = "Yes" Then 'check if yes
Target.Offset(0, 1).Interior.ColorIndex = 6
ElseIf Target.Value = "No" Then 'check if no
Target.Offset(0, 1).Interior.ColorIndex = 3
Else
Target.Offset(0, 1).Interior.ColorIndex = xlNone
End If
End Sub
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句