vba 尝试从函数返回计算值时出现“编译错误:预期:语句结束”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21341484/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 01:32:01  来源:igfitidea点击:

Getting a "Compile Error: Expected: End of Statement" when trying to return a calculated value from a function

excelvba

提问by ilarson007

I have coded a function that finds the first column that has an empty cell in the third row, but I cannot figure out how to return that integer.

我编写了一个函数,用于查找第三行中有一个空单元格的第一列,但我不知道如何返回该整数。

Function FindEmptyColumn()
' Find the first empty column to paste data into

    ThisWorkbook.Activate
    Worksheets("Black Cam Raw Data").Activate
        Dim dCounter As Double
    dCounter = 1
    Dim iColumn As Integer
    Do Until Cells(3, dCounter) = ""
        iColumn = dCounter
        dCounter = dCounter + 1
    Loop
    iColumn = iColumn + 1
Return iColumn

End Function

Sub CMM_93Cam(ByVal wbTempName As Workbook)
'
' CMM_93Cam Macro
' This subroutine imports data from the CMM Report for a 195C93 Cam flatness measurement into this file.
'
    iColumn = FindEmptyColumn()

Basically, I get an error when I type the "Return iColumn" statement. How do I get the function to return the value I have stored in iColumn?

基本上,当我输入“Return iColumn”语句时出现错误。如何让函数返回我存储在 iColumn 中的值?

回答by mwolfe02

The Returnstatement in VBA does not return the results of a function. Use the function name instead.

ReturnVBA 中的语句不返回函数的结果。请改用函数名称。

Instead of:

代替:

Return iColumn

Use:

用:

FindEmptyColumn = iColumn


In VBA, the Returnstatement is only used in conjunction with GoSub.

在 VBA 中,该Return语句仅与GoSub.

MSDN: GoSub...Return Statement

MSDN: GoSub...返回声明