Create defined name based off another cells value

gaudrco

Board Regular
Joined
Aug 16, 2019
Messages
203
Hi,

I am trying to design code to create a defined name for a cell based off the value of another cell. The cell that contains the name that I would like to be the defined name is in cell (“C2”). The cell that I would like to obtain the defined name is the cell above the new column create from this code:

Code:
Private Sub AddCompetitorButton_Click()

With Sheets("Competitor Overview Data").ListObjects("CompOverviewTable")
    .ListColumns.Add(.ListColumns.Count + 1).Name = Range("C2")
    .ListColumns(.ListColumns.Count - 1).Range.EntireColumn.Copy
    .ListColumns(.ListColumns.Count).Range.EntireColumn.PasteSpecial Paste:=xlPasteFormats
End With

The column headers are competitor names, so I want the cell above the column headers to have the defined name of the competitor name, which comes from cell (“C2”).

What do you think?
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
I have a command button that creates a new column for a newcompetitor. The button is on sheet ‘Instructions’. On this sheet, a person cantype a new competitor into cell C2 and then click the button. This creates anew column on another sheet with the header of that new column as the value ofC2. I would like to add code to this macro to create a defined name on the cellabove the new column header. The defined names for those cells will be thevalue of C2.

The purpose of this is because I have a dynamic image on adifferent sheet that shows the company logos. The logo changes based off a datavalidation list of values. The dynamic image references the list of valuesbased off the defined names.

Here is a visual explanation

A person types "CompNameThree'in cell C2 of the 'Instructions' tab. Whenthe button is pressed, a new column is add to the CompetitorTable on the"Competitor Overview Data' tab

The cell above the header will have thecompany's logo. This cell needs to have a defined name = C2


CompetitorTable
[TABLE="width: 294"]
<colgroup><col width="129" style="width: 97pt; mso-width-source: userset; mso-width-alt: 4579;" span="2"> <col width="133" style="width: 100pt; mso-width-source: userset; mso-width-alt: 4721;"> <tbody>[TR]
[TD="width: 129, bgcolor: white"]Defined Name = CompNameOne[/TD]
[TD="width: 129, bgcolor: white"]Defined Name = CompNameTwo[/TD]
[TD="width: 133, bgcolor: white"]Defined Name = CompNameThree[/TD]
[/TR]
[TR]
[TD="bgcolor: white"]CompNameOne[/TD]
[TD="bgcolor: white"]CompNameTwo[/TD]
[TD="bgcolor: white"]CompNameThree[/TD]
[/TR]
[TR]
[TD="bgcolor: white"]Data[/TD]
[TD="bgcolor: white"]Data[/TD]
[TD="bgcolor: white"]Data[/TD]
[/TR]
[TR]
[TD="bgcolor: white"]Data[/TD]
[TD="bgcolor: white"]Data[/TD]
[TD="bgcolor: white"]Data[/TD]
[/TR]
[TR]
[TD="bgcolor: white"]Data[/TD]
[TD="bgcolor: white"]Data[/TD]
[TD="bgcolor: white"]Data
[/TD]
[/TR]
[TR]
[TD="bgcolor: white"]Data[/TD]
[TD="bgcolor: white"]Data[/TD]
[TD="bgcolor: white"]Data[/TD]
[/TR]
[TR]
[TD="bgcolor: white"]Data[/TD]
[TD="bgcolor: white"]Data[/TD]
[TD="bgcolor: white"]Data[/TD]
[/TR]
</tbody>[/TABLE]
 
Upvote 0
Try adding this line just before the End With
Code:
   .ListColumns(.ListColumns.Count).Range.Offset(-1).Cells(1, 1).Name = Range("C2")
 
Upvote 0
This did not work but I felt like it was close. The error I received was "Run-time error '1004': Application-defined error". Thank you for trying. Any other ideas?
 
Upvote 0
What is the range of the table?
 
Upvote 0
I figured it out using a part of your code and part of another source. Here is the code I used

Code:
With Sheets("Competitor Overview Data").ListObjects("CompOverviewTable")
    .ListColumns.Add(.ListColumns.Count + 1).Name = Range("C2")
    .ListColumns(.ListColumns.Count - 1).Range.EntireColumn.Copy
    .ListColumns(.ListColumns.Count).Range.EntireColumn.PasteSpecial Paste:=xlPasteFormats
    Names.Add Name:=Range("C2"), RefersTo:=.ListColumns(.ListColumns.Count).Range.Offset(-1).Cells(1, 1)
 
Upvote 0
Glad you sorted it & thanks for the feedback
 
Upvote 0
Would you have any idea how to changethe scope of the defined name? With this code, the scope is the worksheet but Iwould like the scope to be the Workbook if I could.

 
Upvote 0
When I run that code the range is workbook scope.
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,289
Members
452,631
Latest member
a_potato

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