在周五的一些好建议帮助我修复了我的VBA代码之后,我想我会尝试使用类似的用户定义函数。这里的想法是获取值列表和(可选)表引用(例如“t”)以最终得到如下字符串: t.value1 + t.value2 + t.value3
它编译得很好,我已经检查了它是否有错字和错误的名字(虽然我可能仍然遗漏了一些东西)。当我尝试在工作表中使用它时,我得到一个“值!”错误。以下代码保存在Excel中的VBA编辑器中的模块中。
提前感谢任何建议。
(请原谅我的“VBA for dummies”风格评论 - 这是因为我是 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
答案 0 :(得分:2)
您只是错过了一点错误处理。在您的代码发生之后,结果将被设置为错误值,因为您要么:
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
需要注意的是,在构建字符串之后设置函数的返回值:
ConcatenateToAdd = strResult1
您可以在Exit Function
作为下一行,但是如果您让执行进入ErrHandler
块,那么您应该只更新ConcatenateToAdd
的值是一个错误。您可以使用以下方式处理:
If Err.Number <> 0 Then
ConcatenateToAdd = CVErr(xlErrValue)
End If