数组范围

时间:2013-09-09 17:47:56

标签: excel-vba vba excel

我最近得到了加快我公司的一个大型(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打印时,数组的值是这样的。我假设它的数组写得不正确,我只是无法找到解决方法。任何建议表示赞赏。谢谢!

1 个答案:

答案 0 :(得分: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