我在Sheet 1 VBA窗口中有代码 . 工作簿中的Excel工作表1带有C列中的下拉列表 . 下拉列表中的4个选项包括:完成,待定,错过截止日期和可工作 . 下拉列表使用工作表2并定义名称方法 . 但是,当我选择值例如“完成”时,整行的颜色不会变为绿色 . 我哪里错了?
Private Sub Worksheet_Change(ByVal Target As Range)
'to make entire row green when job is workable
If Selection.Text = "Workable" Then
With ActiveCell
Range(Cells(.Row, .CurrentRegion.Column), Cells(.Row, .CurrentRegion.Columns.Count + .CurrentRegion.Column - 1)).Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 5287936
.TintAndShade = 0
.PatternTintAndShade = 0
End With
End With
' to make entire row yellow when pending additonal information
ElseIf Selection.Text = "Pending" Then
With ActiveCell
Range(Cells(.Row, .CurrentRegion.Column), Cells(.Row, .CurrentRegion.Columns.Count + .CurrentRegion.Column - 1)).Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 65535
.TintAndShade = 0
.PatternTintAndShade = 0
End With
End With
'to make entire row red when job is not workable
ElseIf Selection.Text = "Missed Deadline" Then
With ActiveCell
Range(Cells(.Row, .CurrentRegion.Column), Cells(.Row, .CurrentRegion.Columns.Count + .CurrentRegion.Column - 1)).Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 255
.TintAndShade = 0
.PatternTintAndShade = 0
End With
End With
'to make entire row light blue when job is complete
ElseIf Selection.Text = "Complete" Then
With ActiveCell
Range(Cells(.Row, .CurrentRegion.Column), Cells(.Row, .CurrentRegion.Columns.Count + .CurrentRegion.Column - 1)).Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent1
.TintAndShade = 0.799981688894314
.PatternTintAndShade = 0
End With
End With
MsgBox "AWESOME!YOU DID IT!"
End If
End Sub
请参阅代码并提供帮助 . 非常感谢!
3 回答
详细说明上述评论
Nabeela,
我建议您切换到条件格式来完成此任务,而不是编写宏 .
您可以添加4种样式,每种颜色一种,并选择基于公式的条件,并添加公式(考虑 N is the column with the status and 5 is the 1st row of the table ,替换为您的值):
如果您需要OR条件,您可以使用
如果您需要AND条件,请使用
然后将样式应用于整个表格 .
.
Considering your comment ,看看这部分:
我会改变这个
试试这个:
如果多个单元格同时更改(例如使用复制和粘贴),并且如果单元格值不在列表中,则将颜色重置为
xlNone
(白色),以使上面的代码工作: