如何使用数组循环分散选择的单元格

时间:2014-11-05 16:24:13

标签: arrays excel vba loops excel-vba

我有一个散布单元格的电子表格,我希望在粗边框中勾勒出轮廓。我把细胞放在一个阵列中。有些是单个细胞,有些是连续分组。因为添加这些边框的代码很长,所以我想循环遍历带边框的单元格。

我尝试选择单元格的行是使用我编写的语法,它显然不起作用。是否有任何语法可行,或者我是否以错误的方式处理问题?


arrCellBorders = Array("A2", "A3", "A6", "B5", "G1", "E7:E10", "E19:E22", "E33:E36", "I7:I10", "I19:I22", "I33:I36", "K7:K10", "K19:K21", "K33", "O7:O10", "O19:O21", "O33", "Q7", "Q9:Q10", "U7", "U9:U10")

For iCounter = 0 To 20

    Range("arrCellBorders(iCounter)").Select
    With Selection.Borders(xlEdgeLeft)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlMedium
    End With
    With Selection.Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlMedium
    End With
    With Selection.Borders(xlEdgeBottom)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlMedium
    End With
    With Selection.Borders(xlEdgeRight)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlMedium
    End With

Next iCounter

2 个答案:

答案 0 :(得分:2)

或者,只是:

arrCellBorders = Array("A2", "A3", "A6", "B5", "G1", "E7:E10", "E19:E22", "E33:E36", _
                        "I7:I10", "I19:I22", "I33:I36", "K7:K10", "K19:K21", "K33", "O7:O10", _
                        "O19:O21", "O33", "Q7", "Q9:Q10", "U7", "U9:U10")

For iCounter = LBound(arrCellBorders) To UBound(arrCellBorders)
    Range(arrCellBorders(iCounter)).BorderAround LineStyle:=xlContinuous, ColorIndex:=0, Weight:=xlMedium
Next iCounter

答案 1 :(得分:1)

尝试一下:

Sub qwerty()
    arrCellBorders = Array("A2", "A3", "A6", "B5", "G1", "E7:E10", "E19:E22", "E33:E36", "I7:I10", "I19:I22", "I33:I36", "K7:K10", "K19:K21", "K33", "O7:O10", "O19:O21", "O33", "Q7", "Q9:Q10", "U7", "U9:U10")
    For i = 0 To 20
        Range(arrCellBorders(i)).Select
        With Selection.Borders(xlEdgeLeft)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlMedium
        End With
        With Selection.Borders(xlEdgeTop)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlMedium
        End With
        With Selection.Borders(xlEdgeBottom)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlMedium
        End With
        With Selection.Borders(xlEdgeRight)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlMedium
        End With
    Next i
End Sub