簡體   English   中英

如何在Excel 2016中調試普通的vba函數?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM