我有以下代码,但我得到了一个" Object Required"第Set CellRange = wks.Range(srange).Select
行上的错误。
我尝试了很多东西,但无法弄清楚。我想根据特定的单元格值在所选范围上设置背景颜色。
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
答案 0 :(得分:2)
根据我的评论,已经获得了太多赞誉,请改变这一点:
Set CellRange = wks.Range(srange).Select
要:
Set CellRange = wks.Range(srange)
删除.Select
。一个人没有使用.Select
funstion设置范围对象。只需将范围对象设置为范围。
答案 1 :(得分:-3)
选择单元格不会返回任何内容。