一、获取单元格的备注
PRivate SubCommandButton1_Click()
Dim strGotIt As String
strGotIt
= WorksheetFunction.Clean(Range("A1").Comment.Text)
MsgBox strGotIt
End Sub
Range.Comment.Text用于得到单元格的备注文本,如果当前单元格没有添加备注,则会引发异常。注意代码中使用了WorksheetFunction对象,该对象是Excel的系统对象,它提供了很多系统函数,这里用到的Clean函数用于清楚指定文本中的所有关键字(特殊字符),具体信息可以查阅Excel自带的帮助文档,里面提供的函数非常多。下面是一个使用application.WorksheetFunction.Substitute函数的例子,其中第一个Substitute将给定的字符串中的author:替换为空字符串,第二个Substitute将给定的字符串中的空格替换为空字符串。
Private FunctionCleanComment(author As String, cmt As String) As String
Dim tmp As String
tmp = Application.WorksheetFunction.Substitute(cmt, author & ":", "")
tmp = Application.WorksheetFunction.Substitute(tmp, Chr(10), "")
CleanComment = tmp
End Function
二、修改Excel单元格内容时自动给单元格添加Comments信息
Private SubWorksheet_Change(ByVal Target As Excel.Range)
Dim newText As String
Dim oldText As String
For Each cell In Target
With cell
On Error Resume Next
oldText = .Comment.Text
If Err <> 0 Then .AddComment
newText = oldText & " Changed by " & Application.UserName & " at " & Now & vbLf
MsgBoxnewText
.Comment.Text newText
.Comment.Visible = True
.Comment.Shape.Select
Selection.AutoSize = True
.Comment.Visible = False
End With
Next cell
End Sub
Comments内容可以根据需要自己修改,Worksheet_Change方法在Worksheet单元格内容被修改时执行。
三、改变Comment标签的显示状态
SubToggleComments()
If Application.DisplayCommentIndicator = xlCommentAndIndicator Then
Application.DisplayCommentIndicator = xlCommentIndicatorOnly
Else
Application.DisplayCommentIndicator = xlCommentAndIndicator
End If
End Sub
Application.DisplayCommentIndicator有三种状态:xlCommentAndIndicator-始终显示Comment标签、xlCommentIndicatorOnly-当鼠标指向单元格的Comment pointer时显示Comment标签、xlNoIndicator-隐藏Comment标签和单元格的Comment pointer。
四、改变Comment标签的默认大小
SubCommentFitter1()
With Range("A1").Comment
.Shape.Width = 150
.Shape.Height = 300
End With
End Sub
注意:旧版本中的Range.NoteText方法同样可以返回单元格中的Comment,按照Excel的帮助文档中的介绍,建议在新版本中统一使用Range.Comment方法。
新闻热点
疑难解答