简体   繁体   English

UDF命名单元格

[英]UDF to name cells

I'm trying to build a UDF function to name cells in a spreadsheet. 我正在尝试构建UDF函数来命名电子表格中的单元格。

I've tried the following, but it doesn't work: 我已经尝试了以下方法,但是不起作用:

Function NameThatCell(TheCell As Range, TheName As String)
    TheCell.Name=TheName
End Function

You may want to spend some time learning about the difference between a function and a subroutine. 您可能需要花一些时间来了解函数和子例程之间的区别。

A function returns a value. 函数返回一个值。 A function has one or more inputs, then performs a calculation and returns the result of the calculation to the calling instance. 一个函数具有一个或多个输入,然后执行计算并将计算结果返回给调用实例。 That can be a worksheet formula or a call within a VBA routine. 这可以是工作表公式,也可以是VBA例程中的调用。 A function should never be used to manipulate elements of the worksheet like cells, range names, objects, etc. 绝对不能使用函数来处理工作表的元素,例如单元格,范围名称,对象等。

A sub (subroutine) can do all kinds of things to elements of the worksheet. 子(子例程)可以对工作表的元素执行各种操作。 It starts, it does things, then it ends. 它开始,做事,然后结束。 It does not return anything and it can work with all aspects of the workbook. 它不返回任何内容,并且可以与工作簿的所有方面一起使用。 It can even call a function if it needs to get the result of a calculation that is performed in the function. 如果需要获取在函数中执行的计算结果,它甚至可以调用函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM