Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

Macro determines the range name of the active cell in Excel


View products that this article applies to.

Summary

This article provides a macro that searches through all the names in a Microsoft Excel workbook to find which name or names the active cell is currently located in. For example, if the current active cell is A5 on sheet1 and there is a defined name "Test" that refers to "sheet1!A1:C5," the macro will display a message stating:
The activecell is in the range named "Test"

↑ Back to the top


More information

Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements.
'This macro searches through all the defined names in a workbook to find
'the name(s) that the activecell appears in. For those names it finds
'it displays a message box with the range name in it
'
Sub Find_Name_ActiveCell_In()
   Dim oCurrentName as Name            
   Dim rRng as Range
'
'Ignore any errors and may occur from some names referencing 
'external workbooks
'
    On Error Resume Next
'
'Loop through all the defined names in the workbook
'
    For Each oCurrentName In Names
'
'make sure the name is a valid cell reference
' 
       Set rRng = Range(oCurrentName.Name)
'
'check the error flag to make sure no error was generated by the
'above line
'
        If Err = 0 Then
'
'Test to see if the active cell is in the current named range
'
           If Not Intersect(ActiveCell, rRng) Is Nothing Then
'
'if it is then display the message box
'
                MsgBox "Activecell is in the range named " & _
                    oCurrentName.Name & """"
            End If
        Else
'
'reset the error flag
'
            Err = 0
        End If
    Next
End Sub

↑ Back to the top


Keywords: KB811438, kbhowto

↑ Back to the top

Article Info
Article ID : 811438
Revision : 7
Created on : 2/1/2007
Published on : 2/1/2007
Exists online : False
Views : 282