好友
阅读权限10
听众
最后登录1970-1-1
|
方法:使用VBA宏批量修改批注
打开VBA编辑器
按 Alt + F11 打开VBA编辑器。
插入新模块
右键左侧项目树 -> 插入 -> 模块。
粘贴以下代码:
Sub BatchModifyCommentFormat()
Dim ws As Worksheet
Dim cmt As Comment
Dim shp As Shape
Dim targetFontName As String
Dim targetFontSize As Integer
Dim targetFontColor As Long
Dim fillColor As Long
Dim borderColor As Long
' 自定义格式参数(按需修改)
targetFontName = "微软雅黑" ' 字体名称
targetFontSize = 10 ' 字号
targetFontColor = RGB(0, 0, 0) ' 字体颜色(黑色)
fillColor = RGB(255, 255, 200) ' 批注背景色(浅黄色)
borderColor = RGB(0, 128, 0) ' 边框颜色(绿色)
On Error Resume Next ' 跳过无批注的单元格
For Each ws In ThisWorkbook.Worksheets
For Each cmt In ws.Comments
' 修改文本格式
With cmt.Shape.TextFrame.Characters.Font
.Name = targetFontName
.Size = targetFontSize
.Color = targetFontColor
.Bold = False
End With
' 修改批注框格式
Set shp = cmt.Shape
With shp
' 背景填充
.Fill.ForeColor.RGB = fillColor
.Fill.Transparency = 0 ' 不透明
' 边框设置
.Line.ForeColor.RGB = borderColor
.Line.Weight = 1.5 ' 边框粗细
End With
Next cmt
Next ws
MsgBox "批注格式已批量修改完成!"
End Sub
4.自定义参数(按需修改代码中的以下部分):
targetFontName:字体名称
targetFontSize:字号
targetFontColor:字体颜色(使用RGB值)
fillColor:批注背景颜色
borderColor:边框颜色
运行宏
返回Excel界面,按 Alt + F8 打开宏对话框。
选择 BatchModifyCommentFormat -> 点击【运行】。
若需要更复杂的格式调整,可在代码中添加以下属性:
' 文字方向(0-正常,1-竖排)
cmt.Shape.TextFrame.Orientation = 0
' 自动调整大小
cmt.Shape.TextFrame.AutoSize = True
' 修改批注框大小
cmt.Shape.Width = 200
cmt.Shape.Height = 50
通过此方法可快速统一所有批注的格式,显著提升工作效率。 |
|