VBA & Macro Problems

Name Excel Columns from Row 1 with a VBA Macro

Khadija KhanKhadija Khan Nov 25, 2026 870 views

Question details

Create a named range for each data column using its row 1 header as the name.

Name Excel Columns from Row 1 with a VBA Macro
Product
Microsoft Excel
Device & OS
not provided
Scenario
A worksheet has headers in row 1 and needs workbook-level names that include each header and the populated cells below it.
Observed behavior
Each named range should extend from its row 1 header through the last used cell in that column.
Before you start

Check that row 1 contains unique, valid Excel names: names cannot contain spaces or look like cell references. Save the workbook as an Excel Macro-Enabled Workbook (.xlsm) and make sure your Excel installation supports VBA macros.

Solution 1Recommended

Create column names with an Excel VBA loop

Use this macro to create workbook-level names for the active worksheet's columns without replacing existing names.

The macro checks each header through the last used cell in row 1 and finds the last used row separately for each column. It skips blank or invalid headers and names that already exist. Cells containing formulas are considered used, even if a formula displays an empty string.

1
Open the VBA editor

Open the workbook in desktop Excel, select the worksheet containing your headers, and open the VBA editor from Developer > Visual Basic.

2
Insert the macro

In the VBA editor, select Insert > Module and paste this code: Sub NameColumnsFromRow1() Dim ws As Worksheet, wb As Workbook Dim lastCol As Long, lastRow As Long, col As Long Dim header As String, created As Long, skipped As Long Dim existingName As Name, newName As Name Set ws = ActiveSheet Set wb = ws.Parent lastCol = ws.Cells(1, ws.Columns.Count).End(xlToLeft).Column For col = 1 To lastCol header = "" If Not IsError(ws.Cells(1, col).Value) Then header = Trim(CStr(ws.Cells(1, col).Value)) End If If Len(header) = 0 Then skipped = skipped + 1 Else Set existingName = Nothing Set newName = Nothing On Error Resume Next Set existingName = wb.Names(header) On Error GoTo 0 If existingName Is Nothing Then lastRow = ws.Cells(ws.Rows.Count, col).End(xlUp).Row On Error Resume Next Set newName = wb.Names.Add(Name:=header, _ RefersTo:="=" & ws.Range(ws.Cells(1, col), _ ws.Cells(lastRow, col)).Address(External:=True)) On Error GoTo 0 If newName Is Nothing Then skipped = skipped + 1 Else created = created + 1 End If Else skipped = skipped + 1 End If End If Next col MsgBox created & " names created; " & skipped & " columns skipped." End Sub

3
Run the macro

Return to Excel, activate the target worksheet, and choose Developer > Macros. Select NameColumnsFromRow1 and click Run. Enable macros only if you trust the workbook and your organization's policy allows it.

4
Check the names

Open Formulas > Name Manager to review the created names and their ranges. If columns were skipped, check their row 1 headers for invalid syntax, duplicates, or existing names.

Create column names with an Excel VBA loop
Ranges do not expand automatically: The macro creates fixed ranges based on the last used row at the time it runs. To include later entries, update the ranges in Name Manager or delete the relevant names and run the macro again.
Free Microsoft Office alternative

Manage Spreadsheet Ranges with WPS Office

WPS Office is a free, lightweight alternative with a familiar spreadsheet interface and Microsoft Office format compatibility for an easier transition. You can define named ranges in WPS Spreadsheets, though this does not fix or guarantee support for the Excel VBA macro.

Define named ranges for spreadsheet columnsCompatible with Microsoft Excel formats such as XLSXFamiliar spreadsheet interface for Office usersFree and lightweight for everyday spreadsheet work
microsoft office alternative - wps office

Frequently Asked Questions

What makes a row 1 header invalid as an Excel name?

Excel names cannot contain spaces or be identical to cell references, such as A1. Use a name such as Sales_Total instead, and check Formulas > Name Manager for existing names.

Does each named range include the header?

Yes. The macro starts each range in row 1 and extends it through the last used cell in that column.

What happens if two columns have the same header?

The macro creates the first available name and skips the later duplicate. Change the duplicate header, then run the macro again; existing names will be skipped rather than replaced.

Will the named ranges grow when I add rows?

No. These are fixed ranges. Use Formulas > Name Manager to change their references, or delete the affected names and rerun the macro after adding data.

Where can I find the names after running the macro?

In Excel, open Formulas > Name Manager to see the workbook names and the cell ranges they refer to.