我有Sheet 1 VBA窗口中的代碼。工作簿中的Excel工作表1與列C中的下拉列表一致。下拉列表中的4個選項爲:完成,待定,錯過截止日期和可行。下拉列表使用表2制定並定義名稱方法。但是,當我選擇值例如「完整」時,整行的顏色不會變成綠色。我哪裏錯了?VBA根據下拉列表值對整個列進行顏色編碼
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
請參閱代碼和幫助。非常感謝!
你只是想改變整個行?使用'EntireRow'並使用'Target'而不是'Selection'。 – SJR
@SJR否,'範圍(單元格(.Row,.CurrentRegion.Column),單元格(.Row,.CurrentRegion.Columns.Count + .CurrentRegion.Column - 1))。選擇'操作符選擇目標區域。 – Eugene
@Eugene - 我認爲還有更好的方法。 – SJR