从函数、子或类型返回多个值? [英] Return multiple values from a function, sub or type?
问题描述
所以我想知道,如何从 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?
推荐答案
您可能想要重新考虑应用程序的结构,如果您真的非常想要一种方法来返回多个值.
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.
' this is the VB6/VBA equivalent of a struct
' data, no methods
Private Type settings
root As String
path As String
name_first As String
name_last As String
overwrite_prompt As Boolean
End Type
Public Sub Main()
Dim mySettings As settings
mySettings = getSettings()
End Sub
' if you want this to be public, you're better off with a class instead of a User-Defined-Type (UDT)
Private Function getSettings() As settings
Dim sets As settings
With sets ' retrieve values here
.root = "foo"
.path = "bar"
.name_first = "Don"
.name_last = "Knuth"
.overwrite_prompt = False
End With
' return a single struct, vb6/vba-style
getSettings = sets
End Function
这篇关于从函数、子或类型返回多个值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!