从VBA中返回数组的值

我已经定义了一个消息函数来放置不同的消息。 这意味着当我以一种forms调用这个函数时,只有相应的select的消息必须显示在屏幕上。 这里我的例子:

'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 

有没有更好的方法来解决它? 目前,我需要在实例化的值之后退出函数。

通常的方式来传递一个索引到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 

请记住,默认的索引是基于零的…………所以期待“保罗”