英文:
VBA script won't run. Getting Run Time Error 424 Object required
问题
我运行时遇到了一个运行时错误。有人能解决吗?
我遇到了一个“对象需要”错误。
英文:
I am getting a run time error when I try to run this. Can anyone solve this?
Sub recolor()
'Sub Color_Change
Dim rng As Range
Set rng = Range("A1:E16")
If Not Intersect(Target, rng) Is Nothing Then
Dim cell As Range
For Each cell In Intersect(Target, rng)
Dim cellValue As String
Dim rgbArray() As String
cellValue = cell.Value
rgbArray = Split(cellValue, ", ")
If UBound(rgbArray) = 2 Then
Dim red As Integer
Dim green As Integer
Dim blue As Integer
red = CInt(rgbArray(0))
green = CInt(rgbArray(1))
blue = CInt(rgbArray(2))
cell.Interior.Color = RGB(red, green, blue)
End If
Next cell
End If
End Sub
I am getting a Object required error
答案1
得分: 1
你只需要处理一个固定范围,不需要Target
:
Sub recolor()
Dim cell As Range, arr
For Each cell In ActiveSheet.Range("A1:E16").Cells '或使用特定工作表
arr = Split(cell.Value, ",")
If UBound(arr) = 2 Then
cell.Interior.Color = _
RGB(CLng(Trim(arr(0))), CLng(Trim(arr(1))), CLng(Trim(arr(2))))
End If
Next cell
End Sub
英文:
You don't need Target
if you just want to process a fixed range:
Sub recolor()
Dim cell As Range, arr
For Each cell In ActiveSheet.Range("A1:E16").Cells 'or use a specific worksheet
arr = Split(cell.Value, ",")
If UBound(arr) = 2 Then
cell.Interior.Color = _
RGB(CLng(Trim(arr(0))), CLng(Trim(arr(1))), CLng(Trim(arr(2))))
End If
Next cell
End Sub
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论