I want to populate the name box for a cell with the value from an adjacent cell. This macro works.
Sub NameBox()
' populates name box w/ value from adjacent cell
ActiveCell.Name = ActiveCell.Offset(0, -1).Value
' steps down to next cell
ActiveCell.Offset(1, 0).Select
End Sub
I assign a key stroke and iterate through each cell in the column which is pretty easy but I think it can be improved with a loop.
I tried this.
Sub NameBoxLoop()
Dim cel As Range
For Each cel In Range("C:C").Cells
If cel.Value <> "" Then
cel.Name = cel.Offset(0, -1).Value
End If
Next cel
End Sub
But I get the following debug error
cel.Name = Application-defined or object-defined error
The loop logic looks right, if I replace the variable cel.Name with cel.Value the loop will complete.
Searches haven't provided an answer to the cel.Name error. Any help in resolving this error is appreciated.
Your formula works. But maybe your environment is not optimal, because you have no error-correction there - and names have limitations.
So I am guessing eigther you don't have values in column B or column B has values which are already used as names for other cells.
In both cases your loop would break.
Try this to loop anyways, but consider error-proving your code:
Sub NameBoxLoop()
On Error Resume Next
Dim cel As Range
For Each cel In Range("C:C").Cells
If cel.Value <> "" Then
cel.Name = cel.Offset(0, -1).Value
End If
Next cel
end sub
Edit:
as a suggestion, you might want to consider using the Names
listing.
Here is an example from excel-help:
ActiveWorkbook.Names.Add Name:="test", RefersTo:="=sheet1!$a$1:$c$20"
And here some of the objectmembers:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.