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方法。

标签:VBA完全操作Excel单元格备注Cell,Comments的代码
0
投稿

猜你喜欢

  • Word2007文档中编辑页眉页脚的操作方法

    2022-03-25 01:07:33
  • Win10专业版应用商店出现错误代码0x80D02017怎么解决?

    2023-11-16 11:20:57
  • 十个经典Office常用技巧

    2023-10-06 16:00:30
  • ​Word文档怎么快速更改图片大小

    2023-08-30 16:38:09
  • word文档无法编辑和复制怎么办 word文档内容无法修改和复制的解决方法

    2022-06-05 21:45:05
  • Office Word打印文件 常见问题汇总

    2023-10-07 03:31:45
  • word教你快速打开最后编辑的文档

    2023-12-05 21:51:19
  • Word2007如何为表格插入题注

    2023-12-10 00:56:03
  • 如何解决Word2007图片显示不全解决的问题

    2023-12-02 18:59:10
  • Win10玩冰汽时代闪退怎么办?Win10玩冰汽时代闪退的解决方法

    2023-12-14 12:29:40
  • 如何在word移动或复制文本

    2023-08-07 07:01:54
  • Word中如何输入小于等于号 Word小于等于号输入方法

    2022-03-15 08:03:16
  • 金山WPS文档怎么添加版权说明

    2023-12-03 02:36:00
  • win10系统夜间模式bug怎么解决

    2023-11-08 17:16:56
  • 迅捷OCR文字识别软件如何将PDF转Word?

    2023-06-10 13:26:40
  • Word中字符间距设置技巧

    2022-10-25 04:43:56
  • Word2016文件怎么缩印?

    2022-03-31 11:17:13
  • Word绘制流程图其实很简单,1分钟教你快速完成制作

    2023-11-09 03:46:12
  • ​Word如何设置文字竖排时数字也竖着排列

    2023-02-15 13:06:05
  • Acrobat怎么开启允许通过用户信息设置图层状态?

    2023-10-21 10:04:14
  • asp之家 电脑教程 m.aspxhome.com