Create cell name from contents of another cell

Gatito

New Member
Joined
Mar 20, 2019
Messages
3
I'm looking for a way to create cell names dynamically. I can find more complex answers to more complex questions, but not this one.

For example: I'm trying to name cell B1 based on the contents of cell A1. And so on: the name of each cell in column B is the contents of each cell immediately to its left (in column A). For example, if A1 contains 'Bananas' then cell B1 takes the name 'Bananas'.

I can see how this can be done in a static fashion - but the key here is that I need a dynamic method. i.e. if the contents of A1 change to 'Apples' then cell B1 is now named 'Apples'.

A wrinkle is that I'm using Excel 2011 (Mac). I will upgrade if need be.
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Place this first code in the sheet code you wish for it to work on.{Right click the sheet, view code, and paste it in}

Code:
Private Sub Worksheet_Change(ByVal Target As Range)    Dim Rng As Range, LastRow As Long


LastRow = Cells(Rows.Count, "A").End(xlUp).Row


    Set Rng = Range("A1:A" & LastRow)
    
    If Not Application.Intersect(Rng, Range(Target.Address)) _
           Is Nothing Then
                Call DelAndRenameRanges
    End If
End Sub

Place the following code in a normal module

Code:
Option Explicit

Sub DelAndRenameRanges()


Dim LastRow As Double, DefName As Name, stName As Variant


stName = ActiveSheet.Name


For Each DefName In Application.ActiveWorkbook.Names
    If InStr(1, DefName, stName) Then
        DefName.Delete
    End If
Next


LastRow = Cells(Rows.Count, "A").End(xlUp).Row


ActiveSheet.Range("A1:B" & LastRow).CreateNames Left:=True


End Sub

I cannot attest to if it will work with excel for Mac 2011, but it does in my copy of excel for Mac 2016
 
Last edited:
Upvote 0
Thank you! I've pasted into sheet code and a normal module respectively. Not doing anything for me yet in Excel 2011, but I'll persevere. Snappy response greatly appreciated. I'll let you know how I get on.
 
Upvote 0
If you wish, I can make the help file I created available to you here
 
Last edited:
Upvote 0
Well, it works perfectly. I'll eventually figure out what I did wrong (I have barely dabbled with VB/macros) but with the answer in hand I am now off to the races - and mighty grateful. Thank you, thank you, thank you.
 
Upvote 0

Forum statistics

Threads
1,223,723
Messages
6,174,113
Members
452,544
Latest member
aush

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top