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

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值