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
Getting a "Compile Error: Expected: End of Statement" when trying to return a calculated value from a function
提问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 Return
statement in VBA does not return the results of a function. Use the function name instead.
Return
VBA 中的语句不返回函数的结果。请改用函数名称。
Instead of:
代替:
Return iColumn
Use:
用:
FindEmptyColumn = iColumn
In VBA, the Return
statement is only used in conjunction with GoSub
.
在 VBA 中,该Return
语句仅与GoSub
.
MSDN: GoSub...Return Statement
MSDN: GoSub...返回声明