2

我最近得到了加快我公司的一个大型(ish)宏的速度的承诺。到目前为止,我已经完成了相当不错的工作,因为最后一位作者或多或少地使用了记录功能(使一些代码无关紧要),这导致我遇到以下格式条件:

With Range("AH10:AP10").Interior
    .Pattern = xlSolid
    .ThemeColor = xlThemeColorDark1
    .TintAndShade = -0.249977111117893
End With
With Range("AH17:AP17").Interior
    .Pattern = xlSolid
    .ThemeColor = xlThemeColorDark1
    .TintAndShade = -0.249977111117893
End With
With Range("AH24:AP24").Interior
    .Pattern = xlSolid
    .ThemeColor = xlThemeColorDark1
    .TintAndShade = -0.249977111117893
End With
With Range("AH34:AP34").Interior
    .Pattern = xlSolid
    .ThemeColor = xlThemeColorDark1
    .TintAndShade = -0.249977111117893
End With
With Range("AH42:AP42").Interior
    .Pattern = xlSolid
    .ThemeColor = xlThemeColorDark1
    .TintAndShade = -0.249977111117893
End With

然后我的问题就变成了,为什么这不起作用?

Dim RangeArr(5) As Variant
RangeArr(0) = Range("AH10:AP10")
RangeArr(1) = Range("AH17:AP17")
RangeArr(2) = Range("AH24:AP24")
RangeArr(3) = Range("AH34:AP34")
RangeArr(4) = Range("AH42:AP42")

For i = 0 To 4
With RangeArr(i).Interior
    .Pattern = xlSolid
    .ThemeColor = xlThemeColorDark1
    .TintAndShade = -0.249977111117893
End With
Next

我得到一个运行时错误 424,循环所需的对象,但使用 msgbox 打印数组的值是这样的。我假设它是写入不正确的数组,我只是不知道如何修复它。任何建议表示赞赏。谢谢!

4

1 回答 1

5

你已经有了一个好的开始!您需要一个范围数组:

Sub human()
    Dim RangeArr(0 To 4) As Range
    Set RangeArr(0) = Range("AH10:AP10")
    Set RangeArr(1) = Range("AH17:AP17")
    Set RangeArr(2) = Range("AH24:AP24")
    Set RangeArr(3) = Range("AH34:AP34")
    Set RangeArr(4) = Range("AH42:AP42")

    For I = 0 To 4
    With RangeArr(I).Interior
        .Pattern = xlSolid
        .ThemeColor = xlThemeColorDark1
        .TintAndShade = -0.249977111117893
    End With
    Next

End Sub
于 2013-09-09T17:56:53.573 回答