VBA 全方位控制 `Comment` 物件:讀取文字、資料變更自動記錄修改者/時間、切換批註指示器模式、設定批註框尺寸。
Dim str As String
str = WorksheetFunction.Clean(Range("A1").Comment.Text)
MsgBox str
`Clean` 移除特殊字元。若儲存格無批註會報錯,需錯誤處理。
Private Sub Worksheet_Change(ByVal Target As Range)
Dim cell As Range, oldTxt As String, newTxt As String
For Each cell In Target
With cell
On Error Resume Next
oldTxt = .Comment.Text
If Err <> 0 Then .AddComment
newTxt = oldTxt & " Changed by " & Application.UserName & " at " & Now & vbLf
.Comment.Text Text:=newTxt
.Comment.Visible = True
.Comment.Shape.Select
Selection.ShapeRange.AutoSize = True
.Comment.Visible = False
End With
Next
End Sub
Sub ToggleComments()
If Application.DisplayCommentIndicator = xlCommentAndIndicator Then
Application.DisplayCommentIndicator = xlCommentIndicatorOnly
Else
Application.DisplayCommentIndicator = xlCommentAndIndicator
End If
End Sub
With Range("A1").Comment.Shape
.Width = 150: .Height = 300
End With
舊版 `NoteText` 已棄用,統一用 `Comment` 物件。