[英]How to force an argument in an Excel UDF
I've created an excel UDF that accepts an array input. 我创建了一个接受数组输入的excel UDF。 I want it only to allow an even number of items in the array.
我希望它只允许数组中的偶数个项目。 Here's the code: (it's only short so I'll post it all and that way you can have some context)
这是代码:(它只是短的所以我会发布所有这些,你可以有一些上下文)
Function SUBSTITUTEMULTI(inputString As String, ParamArray criteria() As Variant) as String
Dim subWhat As String
Dim forWhat As String
Dim x As Single
If UBound(criteria()) Mod 2 = 0 Then
'check whether an even number of arguments is input
MsgBox "You've entered too few arguments for this function", vbExclamation
Else
x = 0
For Each element In criteria
If x = 0 Then
subWhat = element
Else
forWhat = element
inputString = WorksheetFunction.Substitute(inputString, subWhat, forWhat)
End If
x = 1 - x
Next element
SUBSTITUTEMULTI = inputString
End If
End Function
Currently I return a message box that looks like Excel's own one that appears when you enter SUBSTITUTE()
with the third argument missing. 目前,我返回一个消息框,当您输入缺少第三个参数的
SUBSTITUTE()
,该消息框看起来像Excel自己的消息框。 However when you do that with SUBSTITUTE()
or any similar function, Excel prevents you from entering the formula, instead it clicks you back into it so you can fix the faulty function. 但是,当您使用
SUBSTITUTE()
或任何类似函数执行此操作时,Excel会阻止您输入公式,而是单击您重新输入它,以便您可以修复故障功能。 I would like this, as otherwise my function can be pasted in its broken state (odd number of arguments) into several cells, meaning the message box appears several times when recalculating! 我想这样,否则我的函数可以在其破碎状态(奇数个参数)中粘贴到几个单元格中,这意味着重新计算时消息框会出现几次!
How can I fix the code so that if incorrect arguments are detected (an odd number of items in the array) ,then the user is automatically returned to the editing formula step? 如何修复代码,以便在检测到错误的参数(数组中奇数个项目)时,用户是否会自动返回到编辑公式步骤?
Have the function return an error when an incorrect number of arguments are entered. 输入错误数量的参数时,该函数会返回错误。
The return will have to be a Variant rather than String. 返回必须是Variant而不是String。
Substitute your msgbox with SUBSTITUTEMULTI=CVErr(xlErrValue)
. 用
SUBSTITUTEMULTI=CVErr(xlErrValue)
替换你的msgbox。
A list of errors: 错误列表:
xlErrDiv0
for a #DIV/0
error xlErrDiv0
#DIV/0
错误 xlErrNA
for a #N/A
error xlErrNA
#N/A
错误 xlErrName
for a #NAME?
#NAME?
xlErrName
#NAME?
error xlErrNull
for a #NULL
error xlErrNull
一个#NULL
错误 xlErrNum
for a #NUM
error xlErrNum
表示#NUM
错误 xlErrRef
for a #REF
error xlErrRef
表示#REF
错误 xlErrValue
for a #VALUE
error xlErrValue
表示#VALUE
错误 ( http://www.cpearson.com/excel/writingfunctionsinvba.aspx ) ( http://www.cpearson.com/excel/writingfunctionsinvba.aspx )
This isn't a particularly good solution. 这不是一个特别好的解决方案。 Generally the use of
SendKeys
is discouraged and this solution only works if you have disabled your MoveAfterReturn
property (ie unchecked in the image below) 通常不鼓励使用
SendKeys
,只有在禁用MoveAfterReturn
属性后才能使用此解决方案(即在下图中未选中)
Function test(rng As Range)
'check condition
If rng.Count <> 2 Then
MsgBox "Incorrect..."
SendKeys "{F2}", True
Exit Function
End If
'code here to be run if parameters are valid
test = "Success"
End Function
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.