检查单元范围后,没有一个范围名称
我正在尝试根据电子表格上的文本值循环选择细胞并重命名这些单元格。
Sub naming()
Dim cel As Range
Dim selectedRange As Range
Dim to_offset As Integer
Set selectedRange = Application.Selection
Answer = InputBox("Column Where Named Are?")
col_number = Range(Answer & 1).Column
For Each cel In selectedRange.Cells
cel.Name.Delete
to_offset = col_number - cel.Column
cel.Name = cel.Offset(0, to_offset).Value
Next cel
End Sub
删除命令是问题 - 因此,我想我会使用Len()检查名称,但要获得1004错误。
如果没有为单元格定义的名称,则它可以工作(但我不能将删除代码留在中)。
如果已经为单元格定义了名称(并且我使用删除)。
我需要将删除用于现有名称 - 但要跨越空白名称。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一种快速而肮脏的方法是在错误简历上
之间包裹有问题的
行错误恢复Next 告诉VBA忽略错误并继续。在某些情况下,这很有用。大多数时候,您应该避免使用它。我认为这可能是您可以使用它的情况。
或者,您将代码包裹在子中
并使用它,
但是在这种情况下,这是恕我直言不讳的。
在错误处理上的进一步阅读
A quick and dirty way would be to wrap the line in question between
On Error Resume Next
andOn Error Goto 0
, so the code would look like thatUsing
On Error Resume Next
tells VBA to ignore the error and continue on. There are specific occasions when this is useful. Most of the time you should avoid using it. I think this might be a case where you could use it.Or you wrap the code in a sub
and use it like that
But in this case this is IMHO not much of a difference.
Further reading on Error handling
中删除名称的快速而直接的方法。
除了@storax'ES解决方案外,您可能会受益于相对未知函数 /en-us/office/vba/api/excel.range.value“ rel =” nofollow noreferrer“>
rng.value(xlrangevaluexmlspreadsheet)
所选范围的所有现有名称为XML字符串。这节省了进一步的错误处理。将它们分配给数组并在循环中删除以下内容:
help函数
getNames()
应用一些 xml 逻辑,包括命名空间, XPATH搜索字符串*允许提取指定范围的所有名称,并返回所有现有名称的数组。
此XML的样品提取物可能是:
A quick and direct way to delete names in selection
In addition to @Storax 'es solution you might benefit from the fact that the relatively unknown function
rng.Value(xlRangeValueXMLSpreadsheet)
(analyzing the entire sheet structure) returns also all existing names of a selected range as XML string. This saves further error handling.Assign them to an array and delete them in a loop as follows:
Help function
GetNames()
Applying some XML logic including namespaces via XPath search string* allows to extract all names of the specified range and to return an array of all existing names.
A sample extract of this xml might be: