VBA脚本无法运行。出现运行时错误 424,需要对象。

huangapple go评论96阅读模式
英文:

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

huangapple
  • 本文由 发表于 2023年6月6日 05:31:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76410128.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定