VBA删除图片批注保留文字批注的子程序

作者:仇朝权 时间:23-09-02 阅读数:334人阅读

前面介绍了为单元格增加图片批注,既然可以增加,就可以删除。

下面的删除图片批注代码,支持多选单元格、整列选择,只删除图片批注,保留文字批注。

原创代码,转载请注明出处。使用请留言。直接调用即可。

此功能对应MQCal工程算量的此处功能:

delpic.png

Sub DelImgComm()
Rem 删除图片批注背景,保留文本批注
Rem 增加多选区域,日期:2023年8月12日
    On Error Resume Next
    Dim oldCommStr$, Sel, i&
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    If Selection.Count > 1048576 * 2 Then '2023年8月12日优化,否则死机
        MsgBox "选择数量太大,请勿全选。" & Chr(10) & "您可以直接选择壹整列。" & Chr(10) & "或者:" & Chr(10) & "选择的单元格总数量≤100万。"
    ElseIf Selection.Count > 1 Then     '//多选
        i = 0
        For Each Sel In Selection
            If Sel.Comment Is Nothing Then
                If Len(Sel.Value) = 0 Then i = i + 1 Else i = 0  '连续1000个cell为空,表示后面无数据直接跳出 2023年8月12日17点49分
                If i = 1000 Then GoTo EndLine
                GoTo nextSel
            End If
            oldCommStr = ""
            i = 0   '配合确保是连续500个为空
            With Sel
                oldCommStr = .Comment.Text
                .ClearComments  '删除 批注
                If Len(oldCommStr) > 0 Then
                    .ClearComments  '删除所有批注,文本,图片
                    .AddComment
                    .Comment.Text Text:=oldCommStr  '保留文本批注
                    .Comment.Visible = False
                    .Comment.Shape.TextFrame.AutoSize = True    '自动大小
                End If
            End With
nextSel:
        Next Sel
    ElseIf Selection.Count = 1 Then                            '//单选
        'Selection.Address
        oldCommStr = ""
        With Selection
            If .Comment Is Nothing Then GoTo EndLine
            oldCommStr = .Comment.Text
            .ClearComments  '删除 批注
            If Len(oldCommStr) > 0 Then
                .AddComment
                .Comment.Text Text:=oldCommStr  '保留文本批注
                .Comment.Visible = False
                .Comment.Shape.TextFrame.AutoSize = True    '自动大小
            End If
        End With
    Else
        MsgBox "不支持全选。"
    End If
EndLine:
    Application.ScreenUpdating = True
    Application.EnableEvents = True
End Sub


[本文转自仇朝权随笔_VBA删除图片批注保留文字批注的子程序](http://www.zawen.net/post/12.html)

分享到:

♡♡♡转载请保留上面信息♡♡♡

发表评论