国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 辦公 > Excel > 正文

VBA完全操作Excel單元格備注Cell Comments的代碼

2019-10-25 20:23: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方法。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 富裕县| 上犹县| 响水县| 丹江口市| 宜兰市| 弋阳县| 睢宁县| 德钦县| 屏南县| 西贡区| 萝北县| 金乡县| 静海县| 德昌县| 射洪县| 余庆县| 铜陵市| 兰考县| 乐都县| 黄陵县| 遂昌县| 富川| 张家口市| 镇安县| 依兰县| 梁平县| 民和| 禄丰县| 沙湾县| 伊金霍洛旗| 宝山区| 福安市| 西畴县| 雷州市| 华阴市| 商河县| 永寿县| 噶尔县| 白山市| 昌乐县| 永新县|