VBA完全操作Excel单元格备注Cell Comments的代码
时间:2022-02-22 05:29:54
一、获取单元格的备注
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方法。
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Word2007文档中编辑页眉页脚的操作方法
Win10专业版应用商店出现错误代码0x80D02017怎么解决?
![](https://img.aspxhome.com/file/2023/3/49613_0s.jpg)
十个经典Office常用技巧
Word文档怎么快速更改图片大小
![](https://img.aspxhome.com/file/2023/6/31896_0s.png)
word文档无法编辑和复制怎么办 word文档内容无法修改和复制的解决方法
![](https://img.aspxhome.com/file/2023/8/30768_0s.jpg)
Office Word打印文件 常见问题汇总
![](https://img.aspxhome.com/file/2023/6/34936_0s.jpg)
word教你快速打开最后编辑的文档
Word2007如何为表格插入题注
![](https://img.aspxhome.com/file/2023/4/19374_0s.png)
如何解决Word2007图片显示不全解决的问题
![](https://img.aspxhome.com/file/2023/6/29826_0s.jpg)
Win10玩冰汽时代闪退怎么办?Win10玩冰汽时代闪退的解决方法
![](https://img.aspxhome.com/file/2023/6/51466_0s.jpg)
如何在word移动或复制文本
![](https://img.aspxhome.com/file/2023/3/35403_0s.jpg)
Word中如何输入小于等于号 Word小于等于号输入方法
![](https://img.aspxhome.com/file/2023/1/22591_0s.png)
金山WPS文档怎么添加版权说明
![](https://img.aspxhome.com/file/2023/2/a163992_0s.jpg)
win10系统夜间模式bug怎么解决
![](https://img.aspxhome.com/file/2023/5/48355_0s.jpg)
迅捷OCR文字识别软件如何将PDF转Word?
![](https://img.aspxhome.com/file/2023/1/15101_0s.png)
Word中字符间距设置技巧
![](https://img.aspxhome.com/file/2023/6/24906_0s.jpg)
Word2016文件怎么缩印?
![](https://img.aspxhome.com/file/2023/4/30614_0s.jpg)
Word绘制流程图其实很简单,1分钟教你快速完成制作
![](https://img.aspxhome.com/file/2023/8/17078_0s.jpg)
Word如何设置文字竖排时数字也竖着排列
![](https://img.aspxhome.com/file/2023/4/31634_0s.png)
Acrobat怎么开启允许通过用户信息设置图层状态?
![](https://img.aspxhome.com/file/2023/9/15799_0s.png)