从Word VBA中的Function返回两个值 [英] Returning two values from Function in Word VBA

查看:751
本文介绍了从Word VBA中的Function返回两个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在VBA中编写了一个函数,该函数将计算两个值.有什么办法可以使我从函数中返回这两个值?

I have written a function in VBA which will calculate two values.Is there any way that i can get those two values returned from the function.

推荐答案

在VBA中,您可以通过两种方式返回更多值:返回数组或使用ByRef声明,请参见:

In VBA You can return more values by two ways: return an array or use ByRef declaration, see this:

    'return as array
    Function ReturnTwoValues(x As Long) As Long()
        Dim ret(1) As Long
        ret(0) = x
        ret(1) = x * 2
        ReturnTwoValues = ret
    End Function

    'return parameters declared as ByRef
    Sub ReturnTwoPars(x As Long, ByRef y1 As Long, ByRef y2 As Long)
        y1 = x
        y2 = x * 2
    End Sub


    Sub Test()
        'return an array
        Dim ar() As Long
        ar = ReturnTwoValues(2) 'variable must be the same type as function
        Debug.Print ar(0), ar(1)

        'use ByRef
        Dim y1 As Long
        Dim y2 As Long
        ReturnTwoPars 2, y1, y2
        Debug.Print y1, y2

    End Sub

这篇关于从Word VBA中的Function返回两个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆