2012-07-19 57 views
3

我想要创建一个宏来选择矩形范围的单元格,并将这些单元格中的每一个的名称设置为单元格的值/内容。将每个单元格的名称设置为其内容/值

从我迄今为止的想法来看,虽然cell.Name行出现错误。

Public Sub NameCell() 
Dim rng As Range 
Dim cell As Range 
Set rng = Range("A1:D1") 
For Each cell In rng 
    cell.Name = CStr(cell.Value) 
Next 
End Sub 
+4

不是每一段文本是[有效名称(http://office.microsoft.com/en-us/excel-help/define-and-use-names-in-formulas-HA010147120.aspx#BMsyntax_rules_for_names )。 – GSerg 2012-07-19 17:18:50

回答

-1

要用它们的值替换一个单元格的范围(从范围中删除任何公式),你可以使用类似的东西。

Public Sub NameCell() 
    Dim rng As Range 
    Set rng = Range("A1:D1") 
    rng.Value = rng.Value 
End Sub 
+1

这与[将名称分配给范围](http://office.microsoft.com/en-us/excel-help/define-and-use-names-in-formulas-HA010147120.aspx)有什么关系? – GSerg 2012-07-19 17:25:13

0

这是你的意思吗?

Sub setVal() 
    Range("A1:C6").Select 
    Selection = "value" 
End Sub 
+1

这与[给名称分配范围](http://office.microsoft.com/en-us/excel-help/define-and-use-names-in-formulas-HA010147120.aspx)有关? – GSerg 2012-07-19 17:26:38

+0

啊,我误解了这个问题。 – jrad 2012-07-19 17:27:18

+0

是的,这不是我正在寻找的。不管怎么说,多谢拉 – 2012-07-19 17:30:19

0

我相信这可能对你有用,除非我也误解了这个问题。

Dim r As Range 
Dim cell As Range 

Set r = Sheet1.UsedRange 

For Each cell In r 
    Sheet1.Names.Add Name:=cell.Value, RefersTo:=cell 
Next 

请记住,虽然,你会想检查cell.Value是有效的(没有空格等)的命名范围。

相关问题