本文整理两个excel中使用vba添加批注的案例,分享给大家学习。
vba添加批注案例一:
为选中的一个单元格自动添加批注,批注内容为系统当天日期,然后标注外框大小自动调整为刚好容纳内容即可,因为默认的批注比较大。
效果如下图,比如单击A1,然后自动加批注,选中A4,又自动添加批注,以此类推。
实现上面的效果vba添加批注的代码如下:
Subvba添加批注()
OnErrorResumeNext
ActiveCell.AddComment
WithActiveCell.Comment
.TextCStr(Date)
.Shape.Textframe.AutoSize=True
EndWith
EndSub
vba添加批注案例二:
为B列的姓名使用VBA添加批注,要求批注内容为C列单元格对应的的内容,而且批注框内文字大小为11号字体,不加粗,且随内容的多少自动调整批注框的格式的大小。
相关的代码如下:
Subvba添加批注()
DimstrCommentAsString
DimyWidthAsLong
Endrow=Sheet1.[B65536].End(xlUp).Row
Forsn=2ToEndrow
WithSheet1.Cells(sn,2)
strComment=Sheet1.Cells(sn,3)
If.CommentIsNothingThen'没有备注则添加备注
.AddCommentText:=strComment
.Comment.Visible=False
Else '已经有备注则备注添加内容
.Comment.TextText:=strComment
EndIf
With.Comment.Shape
.Textframe.Characters.Font.Size=11
.Textframe.AutoSize=True
If.Width>250Then
yWidth=.Width*.Height
.Width=150
.Height=(yWidth/200)*1.8
EndIf
EndWith
EndWith
Nextsn
EndSub





