How to give a name to the selected cells range in Excel
Only one line of code is necessary to assign a name to the selected Excel range, as you can see below.
This is very useful for example if you want to use a given range to build several pivot tables, as the name given to the range can be used as the source for the pivot table building wizard. So you don't need to update the source of every pivot table if the source data changes.
VBA code to give a name to the selected area in Excel
' ***************************************************
' ***************************************************
' Function : sel_setName
' Desc : Assign a name to a selection of cells
' Paramters :
' sName : name to be assigned to the selected range of cells
' ***************************************************
Function sel_setName(sName As String)
ActiveWorkbook.Names.Add Name:=sName, RefersToR1C1:=Selection
End Function
How to remove the assigned name to a range of cells
Another line of code is necessary to perform the reverse operation: remove the name assigned to a group of Excel cells.
Remove assigned name to an Excel range
' ***************************************************
' ***************************************************
' Desc.: remove the assignment of the name to an Excel range
' Paramters :
' sName : name to be assigned to the selected range of cells
' ***************************************************
Function sel_deleteName(sName As String)
ActiveWorkbook.Names(sName).Delete
End Function
How to control if a Name is already assigned to an Excel range.
We'll continue exploring the functions allowing to control and manage Excel range names. Next we'll see a function which can tell you if one Name is already assigned to an Excel range (output true) or not (output False)