Dynamically Create Range Name in Excel Sheet VBA Example

Dynamically Create Range Name in Excel Sheet VBA Example

CreateNames

Method creates name ranges by determining labels associated to respective cells in a sheet. CreateNames method exposed by Range object. In this post we will create name ranges for some entities using VBA code.

Syntax

expression.CreateNames(Top, Left, Bottom, Right)

All parameters for CreateNames method are variant type and optional. Top: where excel will guess top cells are label in selection and so on. Below source will create four name. User must select labels and the cells where A2:A5 refers to label and B2:B5 refers to reference cells which will be named. Hence user must make selection to A2:B5

Data source

Code example

Public Sub CreaetNamesExample()
    'Range objec to bind selection
    Dim oRange As Range
    
    'bind selection
    Set oRange = Selection
    
    'Create name and set Left true becuase our labels are located at left side A2:A5
    oRange.CreateNames Left:=True
    
    'Memory cleanup
    Set oRange = Nothing
End Sub

Make Selection and Run above code

Output

Top

Code example

Public Sub CreaetNamesUsingTopExample()
    'Range objec to bind selection
    Dim oRange As Range
    
    'bind selection
    Set oRange = Selection
    
    'Create name and set Top true becuase our labels are located at left side A1:B1
    oRange.CreateNames Top:=True
    
    'Memory cleanup
    Set oRange = Nothing
End Sub

Output

Left is auto guessed and created Apple name range as shown above. Please leave your comments or queries under comment section also please do subscribe to our blogs to keep your self upto date.

Leave a Reply

Your email address will not be published.