假设当前活动工作表中的批注单元格分布如下:
A1 → Area1
B3:B5 → Area2
![](https://video.ask-data.xyz/img.php?b=https://iknow-pic.cdn.bcebos.com/d6ca7bcb0a46f21ff0ec8662ff246b600c33ae23?x-bce-process=image%2Fresize%2Cm_lfit%2Cw_600%2Ch_800%2Climit_1%2Fquality%2Cq_85%2Fformat%2Cf_auto)
Excel VBA 参考代码:
Option Explicit
Public Sub Test()
Dim strName As String
On Error Resume Next
strName = InputBox("请输入单元格名称")
If StrPtr(strName) Then
If Len(Trim(strName)) Then
Dim nmItem As Name
Set nmItem = ActiveSheet.Names(strName)
If Not (nmItem Is Nothing) Then
Dim rngCell As Range
Dim cmtItem As Comment
For Each rngCell In nmItem.RefersToRange.Cells
Set cmtItem = rngCell.Comment
If Not (cmtItem Is Nothing) Then
Debug.Print rngCell.Address & vbTab & cmtItem.Text
Set cmtItem = Nothing
End If
DoEvents
Next
End If
Else
End If
Else
MsgBox "您没有输入要查找的单元格名称!"
End If
End Sub
F5 运行 Test 过程,在 InputBox 对话框中输入 area2:
![](https://video.ask-data.xyz/img.php?b=https://iknow-pic.cdn.bcebos.com/cdbf6c81800a19d8084c5e343afa828ba61e46b0?x-bce-process=image%2Fresize%2Cm_lfit%2Cw_600%2Ch_800%2Climit_1%2Fquality%2Cq_85%2Fformat%2Cf_auto)
可以看到立即窗口(Ctrl+G)输出如下:
![](https://video.ask-data.xyz/img.php?b=https://iknow-pic.cdn.bcebos.com/8d5494eef01f3a29b2660eff9025bc315c607c51?x-bce-process=image%2Fresize%2Cm_lfit%2Cw_600%2Ch_800%2Climit_1%2Fquality%2Cq_85%2Fformat%2Cf_auto)
如果直接点击 InputBox 对话框中的取消或按 ESC 键,则提示:
![](https://video.ask-data.xyz/img.php?b=https://iknow-pic.cdn.bcebos.com/77c6a7efce1b9d166ba04050fadeb48f8c546498?x-bce-process=image%2Fresize%2Cm_lfit%2Cw_600%2Ch_800%2Climit_1%2Fquality%2Cq_85%2Fformat%2Cf_auto)