gpt4 book ai didi

excel - VBA : Choosing discontinuous cell ranges in the loop 中的错误

转载 作者:行者123 更新时间:2023-12-04 21:08:59 25 4
gpt4 key购买 nike

我的要求是根据 C 列单元格中的文本输入更改一行中不同单元格的单元格背景颜色。这适用于工作表中的所有行。
下面是代码:

Private Sub Worksheet_Change(ByVal Target As Range)
Dim i As Long

For i = 4 To 1800
Range("D" & i, "AW" & i).Interior.ColorIndex = 0


If Range("C" & i) = "Open" Then
Range("F" & i & ":H" & i, "K" & i, "L" & i, "AA"&i, "AD"&i, "AF"&i).Interior.ColorIndex = 15
ElseIf Range("C" & i) = "Boil" Then
Range("I" & i, "S" & i).Interior.ColorIndex = 15

End If

Next i

End Sub
但这给出了错误,说“错误数量的参数或无效的属性分配。让我知道我哪里出错了。我是 VBA 新手,这非常令人沮丧。

最佳答案

我不喜欢将太多东西连接在一起,而是使用 token 替换方法:

Private Sub Worksheet_Change(ByVal Target As Range)
Dim i As Long

Me.Range("D4", "AW1800").Interior.ColorIndex = 0 'faster like this...
For i = 4 To 1800
Select Case Me.Range("C" & i).Value
Case "Open"
Me.Range(Replace("F?:H?,K?,L?,AA?,AD?,AF?", "?", i)).Interior.ColorIndex = 15
Case "Boil"
Me.Range("I" & i, "S" & i).Interior.ColorIndex = 15
End Select
Next i

End Sub

关于excel - VBA : Choosing discontinuous cell ranges in the loop 中的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68585746/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com