亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何從VBA函數返回結果

如何從VBA函數返回結果

如何從VBA函數返回結果如何從函數返回結果?例如:Public Function test() As Integer     return 1End Function這給出了編譯錯誤。如何讓這個函數返回一個整數?
查看完整描述

3 回答

?
米脂

TA貢獻1836條經驗 獲得超3個贊

VBA函數將函數名稱本身視為一種變量。因此return,您只需說:而不是使用“ ”語句,而不是

test = 1

但是請注意,這并沒有突破該功能。此語句后的任何代碼也將被執行。因此,您可以使用許多賦值語句來分配不同的值test,并且當您到達函數末尾時的值將是返回的值。


查看完整回答
反對 回復 2019-07-30
?
阿晨1998

TA貢獻2037條經驗 獲得超6個贊

只是將返回值設置為函數名仍然與Java(或其他)語句不完全相同return,因為在java中,return退出函數,如下所示:

public int test(int x) {
    if (x == 1) {
        return 1; // exits immediately
    }

    // still here? return 0 as default.
    return 0;}

在VB中,如果未在函數末尾設置返回值,則精確等效項需要兩行。因此,在VB中,確切的推論看起來像這樣:

Public Function test(ByVal x As Integer) As Integer
    If x = 1 Then
        test = 1 ' does not exit immediately. You must manually terminate...
        Exit Function ' to exit
    End If

    ' Still here? return 0 as default.
    test = 0
    ' no need for an Exit Function because we're about to exit anyway.End Function

既然如此,那么知道你可以像使用方法中的任何其他變量一樣使用return變量也是很好的。像這樣:

Public Function test(ByVal x As Integer) As Integer

    test = x ' <-- set the return value

    If test <> 1 Then ' Test the currently set return value
        test = 0 ' Reset the return value to a *new* value
    End IfEnd Function

或者,返回變量如何工作的極端例子(但不一定是你應該如何實際編碼的一個很好的例子) - 那個會讓你夜不能寐的一個例子:

Public Function test(ByVal x As Integer) As Integer

    test = x ' <-- set the return value

    If test > 0 Then

        ' RECURSIVE CALL...WITH THE RETURN VALUE AS AN ARGUMENT,
        ' AND THE RESULT RESETTING THE RETURN VALUE.
        test = test(test - 1)

    End IfEnd Function


查看完整回答
反對 回復 2019-07-30
  • 3 回答
  • 0 關注
  • 7224 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號