我在 Excel 表中有两列 A 和 B。
列是包含"is"和“否”的下拉列表。
我想根据下拉列表中 A 单元格的文本值更改 B 单元格的颜色。
例如,如果我在 A1 单元格中选择"is",那么 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
使用条件格式。
对于"is",请使用
=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
关于excel - 如何通过VBA根据其他单元格值更改单元格的背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45955832/