UDF函数来命名一个范围

我需要一个小的UDF命名Excel中的范围,但它不断返回#VALUE错误。 做同样的事情作为一个子作品像一个魅力,但不是我想要的,因为我想命名大量的范围。 首先是sub:

Sub setNamedRanges() ' input values inputRange = "A4" newName = "Tank101" ' removing spaces from the name newName = Replace(newName, " ", "") ' write the name Range(inputRange).name = newName End Sub 

现在和函数(inputRange =“A4”和newName =“Tank101”)一样,完成后应该返回“成功”,但根本不工作:

 Function setNamedRange(inputRange, newName) ' removing spaces from the name newName = Replace(newName, " ", "") ' write the name Range(inputRange).name = newName setNamedRange = "succesful" End Function 

我究竟做错了什么? 读入一个数组到所需的值的子将肯定会工作,但没有提供完整的function。

函数不能更改工作簿。 范围是工作簿/图纸属性,因此不能通过函数进行更改。 函数可以返回一个结果到一个工作表,但这不是一个属性,所以没关系。 在反面,subs不能返回任何东西,但是他们可以修改属性。

你可以尝试下面的代码。 它运作良好:

 Sub setNamedRanges() Dim newName As String newName = "Tank101" Dim inputRange As Range Set inputRange = Range("A4") inputRange.name = newName End Sub 

如果您有任何疑虑,请告诉我。

问候

好的,你可以尝试下面的其他方法:

 Sub Button1_Click() Call ChangeValue("Tank101", "Alibaba") End Sub Sub ChangeValue(cellAddress, newValue) Dim inputRange As Range Set inputRange = Range(cellAddress) inputRange.Name = newValue End Sub 

如果您有任何疑虑,请告诉我。

在Marks答案的帮助下,我find了以下解决scheme来解决我的问题,请参阅下面的解决scheme。 这是有点长,不直接,但它的作品。 调用

 giveNameToRange("Tank101") 

在单元格内给这个单元格命名“坦克101”。 代码是:

 Private Declare Function SetTimer Lib "user32" ( _ ByVal HWnd As Long, _ ByVal nIDEvent As Long, _ ByVal uElapse As Long, _ ByVal lpTimerFunc As Long _ ) As Long Private Declare Function KillTimer Lib "user32" ( _ ByVal HWnd As Long, _ ByVal nIDEvent As Long _ ) As Long Private mCalculatedCells As Collection Private mWindowsTimerID As Long Private mApplicationTimerTime As Date Public Function giveNameToRange(newName) As String ' This is a UDF that returns the sum of two numbers and starts a windows timer ' that starts a second Appliction.OnTime timer that performs activities not ' allowed in a UDF. Do not make this UDF volatile, pass any volatile functions ' to it, or pass any cells containing volatile formulas/functions or ' uncontrolled looping will start. newName = Replace(newName, " ", "") giveNameToRange = newName ' Cache the caller's reference so it can be dealt with in a non-UDF routine If mCalculatedCells Is Nothing Then Set mCalculatedCells = New Collection On Error Resume Next mCalculatedCells.Add Application.Caller, Application.Caller.Address On Error GoTo 0 ' Setting/resetting the timer should be the last action taken in the UDF If mWindowsTimerID <> 0 Then KillTimer 0&, mWindowsTimerID mWindowsTimerID = SetTimer(0&, 0&, 1, AddressOf AfterUDFRoutine1) End Function Public Sub AfterUDFRoutine1() ' This is the first of two timer routines. This one is called by the Windows ' timer. Since a Windows timer cannot run code if a cell is being edited or a ' dialog is open this routine schedules a second safe timer using ' Application.OnTime which is ignored in a UDF. ' Stop the Windows timer On Error Resume Next KillTimer 0&, mWindowsTimerID On Error GoTo 0 mWindowsTimerID = 0 ' Cancel any previous OnTime timers If mApplicationTimerTime <> 0 Then On Error Resume Next Application.OnTime mApplicationTimerTime, "AfterUDFRoutine2", , False On Error GoTo 0 End If ' Schedule timer mApplicationTimerTime = Now Application.OnTime mApplicationTimerTime, "AfterUDFRoutine2" End Sub Public Sub AfterUDFRoutine2() ' This is the second of two timer routines. Because this timer routine is ' triggered by Application.OnTime it is safe, ie, Excel will not allow the ' timer to fire unless the environment is safe (no open model dialogs or cell ' being edited). Dim Cell As Range ' Do tasks not allowed in a UDF... Application.ScreenUpdating = False Application.Calculation = xlCalculationManual Do While mCalculatedCells.Count > 0 Set Cell = mCalculatedCells(1) mCalculatedCells.Remove 1 Cell.name = Cell.Value Loop Application.Calculation = xlCalculationAutomatic Application.ScreenUpdating = True End Sub