Sign in with Microsoft
Sign in or create an account.
Hello,
Select a different account.
You have multiple accounts
Choose the account you want to sign in with.

傳回包含使用者指定之 錯誤編號 子類型 錯誤 的 Variant。

語法

CVErr ( 錯誤編號 )

必要 錯誤編號引數 為任何有效的錯誤編號。

註解

使用 CVErr 函數在使用者建立的程式中建立使用者定義的錯誤。 例如,如果您建立接受數個自變數且通常會傳回字串的函數,您可以讓函數評估輸入自變數,以確保它們在可接受的範圍之內。 如果沒有,很可能您的函數不會傳回您預期的結果。 在此事件中, CVErr 可讓您傳回錯誤編號,告訴您要採取什麼動作。

請注意,不允許隱含轉換 錯誤 。 例如,您無法直接將 CVErr 的傳回值指派給不是 Variant 的 變數 。 不過,您可以使用 CIntCDbl 等 (執行明確的轉換) CVErr 傳回的值,並將它指派給適當的 資料類型變數。

範例

附註: 下列範例示範如何在 Visual Basic for Applications (VBA) 模組中使用此函數。 如需使用 VBA 的詳細資訊,請在 [搜尋] 旁的下拉式清單中選取 [開發人員參考],並在 [搜尋] 方塊中輸入一個或多個字詞。

此範例使用 CVErr 函數傳回 VarTypevbError (10) 的 Variant。 如果自變數不是數位,使用者定義 CalculateDouble 函數會傳回錯誤。 您可以使用 CVErr 從使用者定義的程式傳回使用者定義的錯誤,或延後處理運行時間錯誤。 使用 IsError 函數來測試值是否代表錯誤。

' Call CalculateDouble with an error-producing argument.
Sub Test()
Debug.Print CalculateDouble("345.45robert")
End Sub
' Define CalculateDouble Function procedure.
Function CalculateDouble(Number)
If IsNumeric(Number) Then
CalculateDouble = Number * 2 ' Return result.
Else
CalculateDouble = CVErr(2001) ' Return a user-defined error
End If ' number.
End Function

Need more help?

Want more options?

探索訂閱權益、瀏覽訓練課程、瞭解如何保護您的裝置等等。

社群可協助您詢問並回答問題、提供意見反應,以及聆聽來自具有豐富知識的專家意見。

Was this information helpful?

How satisfied are you with the translation quality?
What affected your experience?
By pressing submit, your feedback will be used to improve Microsoft products and services. Your IT admin will be able to collect this data. Privacy Statement.

Thank you for your feedback!

×