2015-02-18
ExcelVBA覚書 セルにコメントをつける
セルにコメント(ふきだし)をつけるやり方。
Dim rng As Range
Dim msg As String
Dim clear_flg As Boolean
'選択中のセルにコメントを追加していく
Set rng = ActiveCell
msg = "あいうえお"
clear_flg = False 'コメントを刷新するときはTrueに変える
On Error Resume Next
If Not rng.Comment Is Nothing Then
If clear_flg Then
rng.Comment.Text msg '刷新
Else
rng.Comment.Text rng.Comment.Text() & vbNewLine
& msg '追記
End If
Exit Sub
End If
'新しいコメントを追加する
With rng.AddComment(msg)
.Shape.TextFrame.Characters.Font.ColorIndex = 2 'フォントは白
.Shape.TextFrame.Characters.Font.Size = 9
.Shape.TextFrame.AutoSize = True
.Shape.Fill.ForeColor.RGB = RGB(0, 0, 0) '背景は黒
.Visible = True
End With
clear_flg=Trueならば、コメントは置き換わるが、Falseならば改行して追記していく。
なので、コメントは自動サイズ設定(AutoSize=True)にしておく。