我已经定义了一个消息函数来在其中放入不同的消息。这意味着当我在表单中调用此函数时,只需在屏幕上显示相应的所选消息。这是我的例子:
'Thats the message sub
Public Sub MsgString(txtNo As Byte)
MsgBox MsgTxt(txtNo)
End Sub
'Thats the message function to return the corresponding value, which is chosen
Private Function MsgTxt(txtNo As Byte) As String
Dim arrMsgTxt() As Variant
arrMsgTxt = Array("error message number one", "error message number two", "error message number three")
For txtNo = LBound(arrMsgTxt) To UBound(arrMsgTxt)
MsgTxt = arrMsgTxt(txtNo)
Exit Function
Next txtNo
End Function
有没有更好的方法来解决它?目前,我需要在实例化的值之后退出该函数。
答案 0 :(得分:2)
将索引传递给UDF的常用方法。这样,您就不需要循环:
Public Function TellMe(indx as Long) as String
ary = Array("Mike", "James", "Paul", "William")
TellMe = ary(indx)
End Function
Sub MAIN()
MsgBox TellMe(2)
End Sub
请记住,默认索引是从零开始的............所以期待" Paul"