excel vba生成具有不同字体颜色的格式单元格
我想用vba在特定布局中生成excel工作表。我的一个子程序是字体颜色。看起来是这样的:excel vba生成具有不同字体颜色的格式单元格,excel,vba,fonts,colors,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
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
但它不起作用。字体始终以黑色生成。我不知道为什么。这对我有效,但有一个逆转
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作为范围
仅cell2
被声明为类型range
,cell1
仍然是一个变体。可以尝试以下方法:
子集合字体(cell1、cell2作为范围、fcolor作为字符串)
范围(单元格1、单元格2)。选择
如果fcolor=“w”,则
有选择。内饰
.ThemeColor=xlThemeColor灯1
.TintAndShade=0
以
ElseIf fcolor=“b”然后
有选择。内饰
.ThemeColor=xlThemeColorDark1
.TintAndShade=0
以
如果结束
End Sub+1对于cell1类型声明注释,这是一个常见问题的好例子