excel vba生成具有不同字体颜色的格式单元格

时间:2011-06-09 13:32:46

标签: excel vba fonts colors

我想用vba在某个布局中生成一个excel表。我的一个子程序是字体颜色。它看起来像这样:

Sub SetFont(cell1, cell2 As range, fcolor As String)

range(cell1, cell2).Select
If fcolor = "w" Then
    With Selection.Font
        .ThemeColor = xlThemeColorLight1
        .TintAndShade = 0
    End With
ElseIf fcolor = "b" Then
    With Selection.Font
        .ThemeColor = xlThemeColorDark1
        .TintAndShade = 0
    End With
End If

End Sub

但它不起作用。字体始终以黑色生成。我不知道为什么。

2 个答案:

答案 0 :(得分:3)

它适用于我,但有一个逆转; xlThemeColorLight1生成深色文字和 xlThemeColorDark1创建了较轻的文本,这非常愚蠢,但你去了。

Sub foo()
SetFont Range("A1"), Range("A6"), "b"
End Sub

Sub SetFont(cell1 As Range, cell2 As Range, fcolor As String)
If fcolor = "w" Then
    With Range(cell1, cell2).Font
        .ThemeColor = xlThemeColorLight1
    End With
ElseIf fcolor = "b" Then
    With Range(cell1, cell2).Font
        .ThemeColor = xlThemeColorDark1
    End With
End If
End Sub

不相关,但如果cell1, cell2 As range cell2仅声明range类型,则cell1仍然是变体。

答案 1 :(得分:0)

可以试试这个:     Sub SetFont(cell1,cell2 As range,fcolor As String)     范围(cell1,cell2)。选择     如果fcolor =" w"然后     用Selection.Interior         .ThemeColor = xlThemeColorLight1         .TintAndShade = 0     结束     如果fcolor =" b"然后     用Selection.Interior         .ThemeColor = xlThemeColorDark1         .TintAndShade = 0     结束     结束如果

End Sub