从函数、子函数或类型返回多个值?
所以我想知道,如何从 VBA 中的函数、子函数或类型返回多个值? 我有这个主子程序,它应该从多个函数收集数据,但一个函数似乎只能返回一个值。那么我怎样才能将多个返回给一个子呢?
So I was wondering, how can I return multiple values from a function, sub or type in VBA?
I've got this main sub which is supposed to collect data from several functions, but a function can only return one value it seems. So how can I return multiple ones to a sub?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
如果您真的非常希望一种方法返回多个值,您可能需要重新考虑应用程序的结构。
要么将事物分开,以便不同的方法返回不同的值,要么找出逻辑分组并构建一个对象来保存可以依次返回的数据。
You might want want to rethink the structure of you application, if you really, really want one method to return multiple values.
Either break things apart, so distinct methods return distinct values, or figure out a logical grouping and build an object to hold that data that can in turn be returned.
您可以尝试返回 VBA 集合。
只要您处理对值,例如“Version=1.31”,您就可以将标识符存储为键(“Version”),并将实际值(1.31)存储为项目本身。
之后访问这些值就轻而易举了:
这有意义吗?
You could try returning a VBA Collection.
As long as you dealing with pair values, like "Version=1.31", you could store the identifier as a key ("Version") and the actual value (1.31) as the item itself.
Accessing the values after that it's a breeze:
Does it make sense?
想法:
Ideas :
您还可以使用变体数组作为返回结果来返回任意值的序列:
丑陋且容易出现错误,因为调用者需要知道返回的内容才能使用结果,但有时还是有用的。
You can also use a variant array as the return result to return a sequence of arbitrary values:
Ugly and bug prone because your caller needs to know what's being returned to use the result, but occasionally useful nonetheless.
函数返回一个值,但它可以“输出”任意数量的值。示例代码:
A function returns one value, but it can "output" any number of values. A sample code:
您可以将 2 个或更多值返回给 VBA 或任何其他 Visual Basic 内容中的函数,但您需要使用称为 Byref 的指针方法。请参阅下面我的示例。我将创建一个函数来添加和减去 2 个值,例如 5,6
you can return 2 or more values to a function in VBA or any other visual basic stuff but you need to use the pointer method called Byref. See my example below. I will make a function to add and subtract 2 values say 5,6
不优雅,但如果您不重叠使用您的方法,您还可以使用全局变量,由代码开头的 Subs 之前的 Public 语句定义。
不过,您必须小心,一旦更改公共值,它将在所有子函数和函数的整个代码中保留。
Not elegant, but if you don't use your method overlappingly you can also use global variables, defined by the Public statement at the beginning of your code, before the Subs.
You have to be cautious though, once you change a public value, it will be held throughout your code in all Subs and Functions.
我总是通过始终返回 ArrayList 来从函数返回多个结果。通过使用 ArrayList,我只能返回一项,其中包含许多多个值,混合在字符串和整数之间。
一旦我在主子中返回了 ArrayList,我只需使用 ArrayList.Item(i).ToString ,其中 i 是索引我想从 ArrayList 返回的值
一个例子:
然后像这样调用函数:
I always approach returning more than one result from a function by always returning an
ArrayList
. By using anArrayList
I can return only one item, consisting of many multiple values, mixing betweenStrings
andIntegers
.Once I have the
ArrayList
returned in my main sub, I simply useArrayList.Item(i).ToString
wherei
is the index of the value I want to return from theArrayList
An example:
And then call the Function like this:
您可以将文件中所需的所有数据连接到单个字符串,并在 Excel 工作表中将其与文本分隔开。
这是我针对同一问题所做的一个示例,请欣赏:
you could connect all the data you need from the file to a single string, and in the excel sheet seperate it with text to column.
here is an example i did for same issue, enjoy: