1
我有以下代码,但在Set CellRange = wks.Range(srange).Select
行上收到“Object Required”错误。Excel VBA选择单元格范围
我已经尝试了很多东西,但无法弄清楚。我想根据特定单元格值在选定范围上设置背景颜色。
Function RowColor()
Application.Volatile
Dim wks As Worksheet
Set wks = ActiveSheet
Dim x As Integer
Dim FirstRow As Integer
Dim CellRange As Range
Dim LastRow As Long
LastRow = wks.Cells(wks.Rows.Count, "A").End(xlUp).Row
FirstRow = 6
'Loop through each row
ScreenUpdating = False
For x = FirstRow To LastRow
Let srange = "A" & x & ":" & "Z" & x
Set CellRange = wks.Range(srange).Select
Select Case wks.Cells(x, "R").Value
Case "O"
CellRange.Interior.Color = RGB(255, 192, 0)
Case "D"
CellRange.Interior.Color = RGB(255, 255, 0)
Case "C"
If wks.Cells(x, "Y") >= 0 Then
CellRange.Interior.Color = RGB(146, 208, 80)
Else
CellRange.Interior.Color = RGB(255, 0, 0)
End If
Case "W"
CellRange.Interior.Color = RGB(0, 176, 240)
End Select
Next x
ScreenUpdating = True
End Function
最后删除'.Select'。 –
@WaiHaLee如果OP想要一个实际的答案,我会的。他们投下的票不是为了“偷”我的答案,而是因为他们的评论不是答案。如果他们把他们的意见归类并删除他们的答案,他们会得到他们的代表点。 –