Excel UDF – 获取VALUE! 错误,不知道为什么

在星期五的一些好的build议帮助我修复了我的VBA代码之后,我想我会尝试一下类似的用户定义函数。 这里的想法是取一个值的列表和(可选的)一个表引用(例如“t”)来结束一个string,如:t.value1 + t.value2 + t.value3

它编译好,我检查了它的错别字和错误的名字(虽然有可能我仍然错过了一些东西)。 当我尝试在工作表中使用它时,我得到一个“VALUE!” 错误。 以下代码保存在Excel中的VBA编辑器中的模块中。

在此先感谢您的任何build议。

(PS原谅我的“VBA傻瓜”风格的评论 – 这是因为我一个VBA的虚拟!)

'Here we'll create the formula's structure - these are the bits the worksheet user will choose: Function ConcatenateToAdd(ConcatenateRange as Range, Optional TableReference as String = "") As Variant 'the default value for TableReference will be "" 'And here are our other building blocks that we'll use behind the scenes: Dim i As Long Dim strResult1 As String 'this will be everything up to the last value Dim strResult2 As String 'this will add the last value on to the string produced as strResult1 Dim Separator1 As String Dim Separator2 As String Separator1 = "." 'this will slip between the table reference and the field name Separator2 = " + " 'this will go after each field name, except the last. 'Just in case - let's make a back-up plan On Error GoTo ErrHandler 'OK - let's go! 'First, let's string together every value but the last one: For i = 1 To ConcatenateRange.Count - 1 strResult1 = strResult1 & TableReference & Separator1 & ConcatenateRange.Cells(i).Value & Separator2 Next i 'Lovely! Now let's just add on the last one - this one won't have a + on the end. For i = ConcatenateRange.Count - 0 To ConcatenateRange.Count + 0 'I'm sure this is not the most elegant way to phrase this... strResult2 = strResult1 & TableReference & Separator1 & ConcatenateRange.Cells(i).Value Next I 'The next bit tells Excel what the final result of the formula should be, in the worksheet: ConcatenateToAdd = strResult2 'And this is what the error handler does - it will just make Excel shout "ERROR!" at you. Let's hope it doesn't need to. ErrHandler: ConcatenateToAdd = CVErr(xlErrValue) 'And that's all! End Function 

你只是错过了一点error handling。 在你的代码中, 无论发生什么,结果都会被设置为一个错误值,因为你可以:

a)在设置了ConcatenateToAdd = strResult2或者之后,不要退出函数

b)检查ErrHandler实际发生的错误

尝试下面的样子 – 我重构了你的代码,因为你不需要两个循环(因此只需要strResult1 ):

 Option Explicit Function ConcatenateToAdd(ConcatenateRange As Range, Optional TableReference As String = "") As Variant On Error GoTo ErrHandler Dim i As Long Dim strResult1 As String Dim Separator1 As String Dim Separator2 As String ' update format if TableReference = "" If TableReference = "" Then Separator1 = "" Else Separator1 = "." End If Separator2 = " + " strResult1 = "" For i = 1 To ConcatenateRange.Count strResult1 = strResult1 & TableReference & Separator1 & ConcatenateRange.Cells(i).Value If i < ConcatenateRange.Count Then strResult1 = strResult1 & Separator2 End If Next i ConcatenateToAdd = strResult1 'you could do an Exit Function here 'Exit Function ' or continue into the ErrHandler block ErrHandler: ' check an error actually occurred If Err.Number <> 0 Then ConcatenateToAdd = CVErr(xlErrValue) End If ' ConcatenateToAdd still equals strResult1 if no error occurred End Function 

需要注意的是,在构buildstring之后,函数的返回值被设置:

 ConcatenateToAdd = strResult1 

你可以做一个Exit Function作为下一行,但如果你让执行滑入ErrHandler块,那么你应该只更新ConcatenateToAdd的值, 如果有错误 。 您可以通过以下方式处理:

 If Err.Number <> 0 Then ConcatenateToAdd = CVErr(xlErrValue) End If