在excel vba上处理错误

Ron*_*nan 1 excel vba excel-vba

我无法弄清楚如何处理我的宏中引发的错误.

通过application.Vlookup我搜索一个值.问题是,如果该值不存在宏停止.

我试过一个On Error Resume Next工作正常,但我想告诉用户该值不存在.

    Private Sub CommandButton1_Click()
    Dim Num As Double
    Dim Cle As Integer
    Dim Dpt As String
    Dim Age As Integer
    Dim Essaidate As String
    Dim CommNaiss As String
    Dim NumOrdre As String
    Dim Reg As String


   'Initialisons la date du jour

    CeJour = Date

    Num = TextBox1.Text

    Cle = 97 - (Num - (Int(Num / 97) * 97))

    If Cle < 10 Then

        Label2.Caption = "0" & Cle
    Else
        Label2.Caption = Cle
    End If

    If Mid(TextBox1.Text, 1, 1) = "1" Then
        Label4.Caption = "Masculin"
    Else
        Label4.Caption = "Féminin"
    End If

    Essaidate = "1" & "/" & Mid(TextBox1, 4, 2) & "/" & "19" & Mid(TextBox1, 2, 2)
    'MsgBox ("La date de naissance (sans le jour) de cette personne est :" & Essaidate)
    Dpt = Application.VLookup(Mid(TextBox1.Text, 6, 2), Range("M1:N96"), 2, False)
    Label6.Caption = Dpt & " (" & Mid(TextBox1.Text, 6, 2) & ")"

    Reg = Application.VLookup(Mid(TextBox1.Text, 6, 2), Range("M1:O96"), 3, False)
    Label15.Caption = Reg

    'On Error Resume Next

     CommNaiss = Application.VLookup(CLng(Mid(TextBox1.Text, 6, 5)), Range("AV1:AW36529"), 2, False) 'That's the line I get an error if value does't exist....
Run Code Online (Sandbox Code Playgroud)

Tim*_*son 5

我会用a GoTo ErrorHandler:,有一个MsgBox,然后继续下一个.

On Error GoTo ErrorHandler

ErrorHandler:
    MsgBox "Value does not exist"
Resume Next
Run Code Online (Sandbox Code Playgroud)