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

我有一个分散的单元格的电子表格,我想在一个厚边框轮廓。 我把单元格放在一个数组中。 有些是个别的细胞,有些是连续的分组。 由于添加这些边框的代码很长,我想循环遍历将有边框的单元格。

我试图select单元格的行正在使用我编写的语法,显然不起作用。 有没有什么语法可以工作,还是我以错误的方式处理问题?


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 

试试这个:

 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 

或者,只是:

 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