Excel VBA批註

1、判斷單元格是否存在批註

Function ISComment(Ranges As Range) As Integer
'判斷單元格區域批註數
    Application.Volatile True   '自動重算
    Dim tempRange As Range
    For Each tempRange In Ranges
        If Not tempRange.Comment Is Nothing Then  'Nothing 判斷一個變量是否爲空
            ISComment = ISComment + 1  '函數返回值
        End If
    Next tempRange
End Function

2、批量插入相同批註

Public Sub 批註插入()
    Dim tempRange As Range, re As Range
    Set tempRange = Application.Selection    ’選擇的單元格區域
    Dim str As String
    str = InputBox("請輸入批註文字:", Title:="批註插入")

    '爲空時退出不插入批註

    '輸入時在前面加一個空格,自動插入一個批註插入時間

    If str = "" Then
        Exit Sub
    End If
    '時間
    Dim i As String
    If VBA.Mid(str, 1, 1) = " " Then
        i = VBA.Now() & ": "
    End If
  '插入批註
    For Each re In tempRange
      'if判斷是否有批註 
      If re.Comment Is Nothing Then
          re.AddComment    '添加批註
          re.Comment.Visible = False    '批註是否可見
          re.Comment.Text Text:=i & str   '批註文字
      Else  '有批註則換行添加
          re.Comment.Text Text:=re.Comment.Text & VBA.Chr(10) & str      VBA.Chr(10)換行
      End If
    Next re
End Sub

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章