本文整理两个excel中使用vba添加批注的案例,分享给大家学习。
vba添加批注案例一:
为选中的一个单元格自动添加批注,批注内容为系统当天日期,然后标注外框大小自动调整为刚好容纳内容即可,因为默认的批注比较大。
效果如下图,比如单击A1,然后自动加批注,选中A4,又自动添加批注,以此类推。

实现上面的效果vba添加批注的代码如下:
Sub vba添加批注()
On Error Resume Next
ActiveCell.AddComment
With ActiveCell.Comment
?.Text CStr(Date)
?.Shape.TextFrame.AutoSize = True
End With
End Sub
vba添加批注案例二:
为B列的姓名使用VBA添加批注,要求批注内容为C列单元格对应的的内容,而且批注框内文字大小为11号字体,不加粗,且随内容的多少自动调整批注框的格式的大小。

相关的代码如下:
Sub vba添加批注()
Dim strComment As String
Dim yWidth As Long
Endrow = Sheet1.[B65536].End(xlUp).Row
For sn = 2 To Endrow
??? With Sheet1.Cells(sn, 2)
???????? strComment = Sheet1.Cells(sn, 3)
???????? If .Comment Is Nothing Then '没有备注则添加备注
??????????? .AddComment Text:=strComment
??????????? .Comment.Visible = False
???????? Else? '已经有备注则备注添加内容
??????????? .Comment.Text Text:=strComment
???????? End If
??????? With .Comment.Shape
??????????? .TextFrame.Characters.Font.Size = 11
??????????? .TextFrame.AutoSize = True
???????????? If .Width > 250 Then
??????????????? yWidth = .Width * .Height
??????????????? .Width = 150
??????????????? .Height = (yWidth / 200) * 1.8
???????????? End If
??????? End With
??? End With
Next sn
End Sub
……