Excel VBA - 财务模型颜色格式
Excel VBA - Financial Model Color Formatting
在金融模型中,通常根据输入对单元格进行颜色编码(例如,参见 here)。我想创建一个宏来为我自动执行此任务。
所需的颜色代码如下
- 蓝色:常量(文本除外)
- 黑色:公式
- 绿色:对其他工作表的引用
- 红色:对单独文件或外部链接的引用
感谢 Rory 和 Samuel 的出色回答,我能够使用以下代码实现上述目标:
Sub financial_color_coding()
' Color hard-coded cells blue
With Selection.SpecialCells(xlCellTypeConstants, 21).Font
.Color = -65536 ' colour selected cells blue
.TintAndShade = 0
End With
' Select cells that contain formulas
Selection.SpecialCells(xlCellTypeFormulas, 23).Select
'Color selected cells based on their input
For Each cell In Selection
If Left(cell.Formula & " ", 1) = "=" Then
If InStr(CleanStr(cell.Formula), "]") Then
cell.Font.Color = RGB(255, 0, 0) ' red for references to other files
ElseIf InStr(CleanStr(cell.Formula), "!") Then
cell.Font.Color = RGB(0, 150, 0) ' green for references to other sheets
Else
cell.Font.Color = RGB(0, 0, 0) 'black for every other formula
End If
End If
Next cell
End Sub
Function CleanStr(strIn As String) As String
Dim objRegex As Object
Set objRegex = CreateObject("vbscript.regexp")
With objRegex
.Pattern = "\""[^)]*\"""
.Global = True
CleanStr = .Replace(strIn, vbNullString)
End With
End Function
运行宏只会更改工作簿中包含常量数字或公式的单元格的字体,并且会保持文本的整体格式不变。
SpecialCells 记录在此处:
https://docs.microsoft.com/en-us/office/vba/api/excel.range.specialcells
然而,并不是所有的事情都可以用它来完成。如果公式包含 !
或 ]
,它会引用另一个 sheet 或文件。 CleanStr
删除引号中的所有文本,因为这些文本也可能包含这些字符。
Selection.SpecialCells(xlCellTypeConstants).Font.Color = RGB(0, 0, 255) 'blue for constant
Selection.SpecialCells(xlCellTypeFormulas).Font.Color = RGB(0, 0, 0) 'black for formulas
'to be more specifiy
For Each cell In Selection
If Left(cell.Formula & " ", 1) = "=" Then
If InStr(CleanStr(cell.Formula), "]") Then
cell.Font.Color = RGB(255, 0, 0) ' red for references to other files
ElseIf InStr(CleanStr(cell.Formula), "!") Then
cell.Font.Color = RGB(0, 150, 0) ' green for references to other sheets
Else
cell.Font.Color = RGB(250, 0, 255) 'pink for formulars with output text
End If
ElseIf Not IsNumeric(cell.Text) Then
cell.Font.Color = RGB(0, 0, 0) 'black for text constant
End If
Next cell
CleanStr
取自这里:
Function CleanStr(strIn As String) As String
Dim objRegex As Object
Set objRegex = CreateObject("vbscript.regexp")
With objRegex
.Pattern = "\""[^)]*\"""
.Global = True
CleanStr = .Replace(strIn, vbNullString)
End With
End Function
在金融模型中,通常根据输入对单元格进行颜色编码(例如,参见 here)。我想创建一个宏来为我自动执行此任务。
所需的颜色代码如下
- 蓝色:常量(文本除外)
- 黑色:公式
- 绿色:对其他工作表的引用
- 红色:对单独文件或外部链接的引用
感谢 Rory 和 Samuel 的出色回答,我能够使用以下代码实现上述目标:
Sub financial_color_coding()
' Color hard-coded cells blue
With Selection.SpecialCells(xlCellTypeConstants, 21).Font
.Color = -65536 ' colour selected cells blue
.TintAndShade = 0
End With
' Select cells that contain formulas
Selection.SpecialCells(xlCellTypeFormulas, 23).Select
'Color selected cells based on their input
For Each cell In Selection
If Left(cell.Formula & " ", 1) = "=" Then
If InStr(CleanStr(cell.Formula), "]") Then
cell.Font.Color = RGB(255, 0, 0) ' red for references to other files
ElseIf InStr(CleanStr(cell.Formula), "!") Then
cell.Font.Color = RGB(0, 150, 0) ' green for references to other sheets
Else
cell.Font.Color = RGB(0, 0, 0) 'black for every other formula
End If
End If
Next cell
End Sub
Function CleanStr(strIn As String) As String
Dim objRegex As Object
Set objRegex = CreateObject("vbscript.regexp")
With objRegex
.Pattern = "\""[^)]*\"""
.Global = True
CleanStr = .Replace(strIn, vbNullString)
End With
End Function
运行宏只会更改工作簿中包含常量数字或公式的单元格的字体,并且会保持文本的整体格式不变。
SpecialCells 记录在此处:
https://docs.microsoft.com/en-us/office/vba/api/excel.range.specialcells
然而,并不是所有的事情都可以用它来完成。如果公式包含 !
或 ]
,它会引用另一个 sheet 或文件。 CleanStr
删除引号中的所有文本,因为这些文本也可能包含这些字符。
Selection.SpecialCells(xlCellTypeConstants).Font.Color = RGB(0, 0, 255) 'blue for constant
Selection.SpecialCells(xlCellTypeFormulas).Font.Color = RGB(0, 0, 0) 'black for formulas
'to be more specifiy
For Each cell In Selection
If Left(cell.Formula & " ", 1) = "=" Then
If InStr(CleanStr(cell.Formula), "]") Then
cell.Font.Color = RGB(255, 0, 0) ' red for references to other files
ElseIf InStr(CleanStr(cell.Formula), "!") Then
cell.Font.Color = RGB(0, 150, 0) ' green for references to other sheets
Else
cell.Font.Color = RGB(250, 0, 255) 'pink for formulars with output text
End If
ElseIf Not IsNumeric(cell.Text) Then
cell.Font.Color = RGB(0, 0, 0) 'black for text constant
End If
Next cell
CleanStr
取自这里:
Function CleanStr(strIn As String) As String
Dim objRegex As Object
Set objRegex = CreateObject("vbscript.regexp")
With objRegex
.Pattern = "\""[^)]*\"""
.Global = True
CleanStr = .Replace(strIn, vbNullString)
End With
End Function