簡體   English   中英

用戶定義的函數在vba中工作正常,而從excel工作表調用時顯示“值”錯誤

[英]User defined function working fine in vba, while when called from excel sheet showing 'Value' error

我制作了一個函數,用於從excel調用,該函數具有兩個字符串類型的參數,並在對Excel工作表的特定范圍中存在的數字進行計數后返回整數值。

從vba代碼調用時此函數正常工作,但從excel工作表調用時顯示“ Value”錯誤

我確實將參數類型更改為range。 但它仍然顯示“值錯誤”

當我只是通過分配而不使用excel函數返回整數值時,它在兩種情況下都有效。 但是,在用戶定義的函數代碼中使用Excel函數時,它再次顯示“值”錯誤。

Public Function countEvent(ByVal name As String, ByVal travelType As String) As Integer

    Dim finalValue, tempValue, i As Integer
    Dim rng As Range

    Dim current As Worksheet
    countEvent = 10
    Set current = ActiveWorkbook.Worksheets("Sheet2")
    Set rng = current.Range("T4:T50000")


    'These two values are pasted in two cells in excel sheet, as these values are further being used by some other formula
    current.Range("T2").Value = name
    current.Range("U2").Value = travelType

    countEvent = Application.WorksheetFunction.Count(rng)

End Function

它已定義為在excel工作表的特定范圍內返回數字,但在excel工作表中顯示“值錯誤”

UDF 無法修改單元格或工作簿的格式,也不能在工作表上移動值 如果刪除下面的行,它應該可以工作。

current.Range("T2").Value = name
current.Range("U2").Value = travelType

編輯
對於您想做的事情,我認為您應該探索Worksheet_Change表事件,例如Worksheet_Change

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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