用于格式化单元格的子例程

时间:2014-02-28 18:41:12

标签: vba excel-vba excel

我正在尝试格式化excel中的单元格

以下是我的代码:

Range("A1:F1").Select
Selection.Font.Bold = True
With Selection.Interior
    .Pattern = xlSolid
    .PatternColorIndex = xlAutomatic
    .Color = 65535
    .TintAndShade = 0
    .PatternTintAndShade = 0
End With

我需要为不同的范围多次重复格式化步骤,我可以调用子程序吗?

1 个答案:

答案 0 :(得分:0)

以下是将格式应用于循环中多个范围的一种方法:

Sub dural()
    Dim r As Range
    ary = Array(Range("A1:Z1"), Range("A3:Z3"), Range("A7:Z7"))
    For i = LBound(ary) To UBound(ary)
        Call FFormat(ary(i))
    Next
End Sub

Sub FFormat(rIn As Variant)
    rIn.Font.Bold = True
    With rIn.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .Color = 65535
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With
End Sub