欢迎登陆真网站,您的到来是我们的荣幸。 登陆 注册 忘记密码? ☆设为首页 △加入收藏
欢迎加入真幸福QQ群
电脑知识: 基础知识 网络技术 操作系统 办公软件 电脑维修 电脑安全 windows7 windows8 windows10 服务器教程 平板电脑 视频播放教程 网络应用 互联网 工具软件 浏览器教程 QQ技巧 输入法教程 影视制作 YY教程 wps教程 word教程 Excel教程 PowerPoint
云南西双版纳特产小花糯玉米真空包装


使用CreateObject创建Excel实例代码
excel Criteria1实例
excel用图表动态反映数学函数y=3x+4的曲线
反正切值函数ATAN的使用
excel正切值函数TAN
excel按筛选颜色的VBA代码
EOMONTH函数的作用及用法
win7电脑中快速切换程序窗口
excel RTD函数实际例子及语法
Excel累加、叠加的计算方法
VBA完全操作Excel单元格备注Cell Comments的代码
【 来源:网络 】【 点击:1 】 【 发布时间:2017_03_03 08:59:59 】

  一、获取单元格的备注

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

本网站由川南居提供技术支持,fkzxf版权所有 浙ICP备12031891号
淳安分站 淳安分站