Home>Blogs>VBA>Create Multiple Names using VBA in Excel
Name Manager
VBA

Create Multiple Names using VBA in Excel

Our daily Excel activities can be automated using VBA. Here we have created an example of creating the Name range in Excel using VBA.

Lets’ say we have below given data for which we have create the Names

Data to create the Names
Data to create the Names

We have created a button on the Home tab and assigned the macro on this button.

Additional button added to Home tab
Additional button added to Home tab

Select the entire data and click on this button. All the names will be created. The first row will be considered headers.

Name Manager
Name Manager

Below is the VBA code to create multiple Names with VBA –

Option Explicit

Sub Create_Name()

Dim rng As Range

Set rng = Selection

Dim i As Integer
Dim n As Long
Dim new_range As Range

Dim col_num As Integer
Dim first_Row As Long
Dim last_row As Long

For i = 1 To rng.Columns.Count
    For n = rng.Rows.Count To 1 Step -1
       col_num = rng.Columns(i).Column
       first_Row = rng.Rows(1).Row
       last_row = rng.Rows(n).Row

       If Cells(last_row, col_num).Value <> "" Then
           Set new_range = Range(Cells(first_Row, col_num), Cells(last_row, col_num))
           new_range.CreateNames Top:=True
           Exit For
      End If
    Next n
Next i

MsgBox "Done"

End Sub

 

Click here to download the Practice file-

 

Watch the step by step video tutorial:

PK
Meet PK, the founder of PK-AnExcelExpert.com! With over 15 years of experience in Data Visualization, Excel Automation, and dashboard creation. PK is a Microsoft Certified Professional who has a passion for all things in Excel. PK loves to explore new and innovative ways to use Excel and is always eager to share his knowledge with others. With an eye for detail and a commitment to excellence, PK has become a go-to expert in the world of Excel. Whether you're looking to create stunning visualizations or streamline your workflow with automation, PK has the skills and expertise to help you succeed. Join the many satisfied clients who have benefited from PK's services and see how he can take your Excel skills to the next level!
https://www.pk-anexcelexpert.com