计算机VBA语言题 求代码怎么写

如题所述

假设当前活动工作表中的批注单元格分布如下:

A1 → Area1

B3:B5 → Area2


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:


可以看到立即窗口(Ctrl+G)输出如下:


如果直接点击 InputBox 对话框中的取消或按 ESC 键,则提示:

温馨提示:答案为网友推荐,仅供参考
第1个回答  2016-11-11
Msgbox 你好