2017-03-27 80 views
2

在周五的一些好的建议,这帮助我修复我的VBA代码之后,我想我会试着用一个类似的用户定义的函数。这里的想法是采取值的列表和(可选的)表引用(例如“t”)以最终字符串结尾: t.value1 + t.value2 + t.value3Excel UDF - 获得价值!错误,不知道为什么

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

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

(附注:原谅我“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 
+1

意识到我错过了一些代码,但已经尝试过,并没有区别。缺少的位是:ConcatenateToAdd = strResult2。我现在已经将它添加到了我的问题中 - 只是想解释一下任何人已经准备好了我最初发布的代码。 –

回答

2

你只是缺少的错误处理一点点。在您的代码无论发生的结果将被设置为一个错误值,因为您:

一)设置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 

要注意的一点是,该函数的返回正在建设的字符串后置:

ConcatenateToAdd = strResult1 

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

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

我会添加一些让'Separator1 =“”'如果'TableReference =“”',因为如果没有表格,你不需要'.'。 – CLR

+0

谢谢你,罗宾 - 那曾经是一种享受!是的,我在最后一个UDF中使用了Exit Function,但这次完全错过了它。感谢帮助我看到木头和树木(或木头上的所有树木?)! –

+0

是的,@CLR - 我刚刚发现了!我会捣鼓逻辑和分隔符......我可以在该节(我设置分隔符的值)附近包装一条If语句,你觉得呢? –

相关问题