你可以做类似的规定:
Sub DeleteTextboxesButKeepComments
Dim bx As Excel.TextBox
Dim oComment As Excel.Comment
Dim dicCommentNames As Object 'Scripting.Dictionary
Application.ScreenUpdating = False
Application.EnableEvents = False
On Error GoTo errHandler
'Build a dictionary of the worksheet's comment's shape names.
Set dicCommentNames = CreateObject("Scripting.Dictionary")
dicCommentNames.CompareMode = VbCompareMethod.vbBinaryCompare
For Each oComment In Target.Worksheet.Comments
dicCommentNames(oComment.Shape.Name) = True
Next oComment
If Intersect(Target, Target.Worksheet.Range("B5:B34")) Is Nothing Then 'Or Target = "" Then
For Each bx In Target.Worksheet.TextBoxes
'Avoid deleting textboxes whose name is among those used for comments.
If Not dicCommentNames.Exists(bx.Name) Then
bx.Delete
End If
Next
End If
Cleanup:
Application.EnableEvents = True
Application.ScreenUpdating = True
Exit Sub
errHandler:
MsgBox Err.Description, vbExclamation + vbOKOnly, "Error"
Resume Cleanup
End Sub
我不知道你想通过测试Target = ""
做什么,但如果有超过1个细胞将无法正常工作目标。让我知道,我会修改我的答案。
获取“无错误的简历”消息框,后面跟着一个无限重复的空白消息框。 'dicCommentNames(oComment.Shape.Name)'应该是参数的迭代而不是'.name'然后'= .name'? – zero
我非常不好; 'Application.ScreenUpdating = True'后添加Exit Sub。我会更新答案。 – Excelosaurus