图表中的Excel VBA列 - 格式化字体,颜色,对齐等

时间:2016-03-03 17:48:54

标签: excel vba excel-vba

我在格式化图表中的列时遇到麻烦,以匹配特定的字体,颜色,对齐等。

此代码专注于A:A列,它适用于工作表中的每个单元格。我只希望它影响A列:

Sub PGMNumber()
'
' PGMNumber Macro
'

'
    Range("A:A").Select
    With Selection.Font
        .Name = "Arial"
        .Size = 10
        .Strikethrough = False
        .Superscript = False
        .Subscript = False
        .OutlineFont = False
        .Shadow = False
        .Underline = xlUnderlineStyleNone
        .Color = -16776961
        .TintAndShade = 0
        .ThemeFont = xlThemeFontNone
    End With
    With Selection.Font
        .Name = "Arial"
        .Size = 10
        .Strikethrough = False
        .Superscript = False
        .Subscript = False
        .OutlineFont = False
        .Shadow = False
        .Underline = xlUnderlineStyleNone
        .Color = -16776961
        .TintAndShade = 0
        .ThemeFont = xlThemeFontNone
    End With
    Selection.Font.Bold = False
    Selection.Font.Bold = True
    With Selection.Font
        .Color = -10477568
        .TintAndShade = 0
    End With
    With Selection
        .HorizontalAlignment = xlLeft
        .VerticalAlignment = xlBottom
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With
    Columns("A:A").ColumnWidth = 9.43
End Sub

这是通过记录我的步骤来完成的。我需要的只是FontName,Color,Bold,Size和Alignment。删除我不需要的内容会破坏代码:(

有人可以帮我简化代码,这样只会影响A列吗?

另外,如何添加选项以包含其他列,例如B,C,D等。

最后,我可以从A6向下,B6向下,C6向下等宏开始格式化吗?这样,图表的标题保持不变。

谢谢!

2 个答案:

答案 0 :(得分:0)

这将从第6行开始格式化A列。那里有两种字体颜色,我删除了第一种颜色。要更改此代码的列,请将Range()中的“A”更改为要定位的列,例如.Range("B6:B" & .Cells(.Rows.Count, "B")等。

Sub PGMNumber()
    ' PGMNumber Macro
        Dim FormatRange As Range
        With ThisWorkbook.ActiveSheet
            Set FormatRange = .Range("A6:A" & .Cells(.Rows.Count, "A").End(xlUp).Row)
        End With
        With FormatRange
            With .Font
                .Name = "Arial"
                .Size = 10
                .Bold = True
                .Color = -10477568
            End With
            .HorizontalAlignment = xlLeft
            .VerticalAlignment = xlBottom
        End With
    End Sub

答案 1 :(得分:0)

您可以使用此宏,其中Range()中的两个单元格是要格式化的范围的左上角单元格和右下角单元格。

Sub PGMNumber()
    With Range(Cells(6, "A"), Cells(Rows.Count, "D"))
        With .Font
            .Name = "Arial"
            .Size = 10
            .Color = -16776961
            .Bold = True
        End With
        .HorizontalAlignment = xlLeft
        .VerticalAlignment = xlBottom
    End With
End Sub
相关问题