[英]How do I debug a trivial vba function in Excel 2016?
我正在尝试调试一个简单的vba函数,该函数返回给定的数组,但是一旦到达空单元格就会停止:
Function Test2(source() As Variant) As Collection
Debug.Print "Hello"
Dim i As Integer
i = 1
Do While Not IsEmpty(source(i))
Test2.Add source(i).Value
Debug.Print source(i).Value
i = i + 1
Loop
End Function
当我通过在单元格中写入例如=Test2(A:A)
将数组传递给此函数时,我得到一个#VALUE!
错误,看不到立即窗口打印任何内容。
与如何使用工具查找问题相比,我对查找此功能的问题不感兴趣。 为什么“ TEST”没有打印到“立即窗口”,并且,如果我的程序中出现语法错误,为什么编译器没有设置断点?
再次查看您的函数,向下查看End Function语句。 为了从函数返回值,您需要将函数名称设置为要返回的值。 因此,如果要返回值“ 1”(不带引号),则结束函数前的最后一行应说...。
Test2 = 1
这是您的#value错误的可能原因之一。 可能还有其他原因。 但先来看一看。
删除source()变体中的括号。 只是来源。 您可以使用Range作为数据类型,而不是Variant,因为您的输入是Range(A:A声明一个新集合,用于向集合中添加值。对于迭代,我使用了For Each方法。希望您可以从中得到关于错误的想法下面的代码
Function Test2(source As Variant) As Collection
Dim c As New Collection
Dim cell As Range
Debug.Print "Hello"
Dim i As Integer
i = 0
For Each cell In source
Debug.Print cell.Value
Debug.Print cell.Count
If IsEmpty(cell) = True Then
Exit For
Else
'Set itm = cell
c.Add cell.Value
End If
Next
Set Test2 = c
End Function
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.