Automating cell naming


Posted by Michael McGinnis on January 08, 2002 6:08 PM

I have a worksheet that contains yearly data in columns and labels on each row like this:
1999 2000 2001
ProfitBizA $5,000 $5,800 $6,000
ProfitBizB $3,400 $3,100 $3,500
ProfitBizC $7,900 $6,700 $8,500

I would like to name each cell by combining the description on the leftmost row with the year label on the top of each column. For ex., the cell containing $5,000 would be named "ProfitBizA1999" or some abbreviation like "PrA1999". So, I do the Insert -> Name -> Create thing and type in "PrA1999". Then I click on the cell to the right and do the Insert -> Name thing again, type in "PrA2000" (or edit the PrA1999 name from the existing list). Unfortunately, I have a worksheet like this that has ten year columns and 80 rows of businesses, meaning I have do the Insert -> Name thing 800 times! Can anybody suggest a macro or formula that would combine the description in the left column with the year in the top column and them apply the concatenated name as a name in the current cell, move one cell to the right, find the new combined name using the leftmost cell and the current top row, insert the new name in the current cell, and then keep repeating this until it ran out of cells with values in them? Thanks in advance for reading my lengthy post and thinking about my problem.

Posted by Jacob on January 08, 2002 7:04 PM

Hi

I think this will do the trick:
Lets say your data is in B2:C50 on "Sheet1" modify as needed

Sub RenameEmAll()


On Error Resume Next

Range("B2:C50").Select

For Each cell In Selection
cell.Activate
MyName = Range("A" & cell.Row).Value & Cells(1, cell.Column).Value
ActiveWorkbook.Names.Add Name:=MyName, RefersTo:="=Sheet1!" & ActiveCell.Address
Next

end sub

HTH

Jacob



Posted by Michael McGinnis on January 09, 2002 11:40 AM

Jacob,
I ran the code today. It worked great! A thousand thanks! If you are ever in the Fresno, CA area, email me and I will buy you a couple pints at our fine microbrewery Butterfields. Thanks again.