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.
 

cooper645

Well-known Member
Joined
Nov 16, 2013
Messages
521
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:

Gatito

New Member
Joined
Mar 20, 2019
Messages
3
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.
 

Gatito

New Member
Joined
Mar 20, 2019
Messages
3
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.
 

Forum statistics

Threads
1,081,770
Messages
5,361,173
Members
400,617
Latest member
barron1

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top