2016-04-07 119 views
1

我無法弄清楚如何處理在我的宏中引發的錯誤。通過application.Vlookup我搜索一個值。問題是,如果該值不存在宏停止。excel vba處理出錯

我試過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.... 
+0

所以,如果有錯誤,你想告訴用戶,而不是'恢復下一個'? –

+0

我回滾了你的編輯。請勿將您的解決方案發布在您的帖子正文中。隨意在其他人之間發佈您的答案,或者通過提升/標記答案爲已接受的方式評分另一張海報。 – CubeJockey

回答

0

這有什麼好處? -

CommNaiss = Application.WorksheetFunction.IfError(_ 
    Application.WorksheetFunction.VLookup(CLng(Mid(TextBox1.Text, 6, 5)) _ 
     , Range("AV1:AW36529"), 2, False), "Error") 
5

我會用一個GoTo ErrorHandler:,有MsgBox,那麼接下來的恢復。

On Error GoTo ErrorHandler 

ErrorHandler: 
    MsgBox "Value does not exist" 
Resume Next 
1

這裏遵循兩種可能的方式

1) 「上的錯誤......」 的方式

On Error Resume Next 
Dpt = Application.VLookup(Mid(TextBox1.Text, 6, 2), Range("M1:N96"), 2, False) 
On Error GoTo 0 
If Dpt = "" Then 
    MsgBox "Value : " & Mid(TextBox1.Text, 6, 2) & " not found in Range(""M1:N96"")" 
Else 
    Label6.Caption = Dpt & " (" & Mid(TextBox1.Text, 6, 2) & ")" 
End If 

2) 「查找」 的方式

Dim found As Range 

Set found = Range("M1:M96").Find(What:=Mid(TextBox1.Text, 6, 2), LookIn:=xlValues, LookAt:=xlWhole) 
If found Is Nothing Then 
    MsgBox "Value : " & Mid(TextBox1.Text, 6, 2) & " not found in Range(""M1:N96"")" 
Else 
    Label6.Caption = Dpt & " (" & Mid(TextBox1.Text, 6, 2) & ")" 
End If 

與同爲Reg

2

Ti M的答案 - 使用錯誤處理是最好的,但如果你想在錯誤恢復下次使用,那麼你可以使用ISERROR:

On Error Resume Next 

     CommNaiss = Application.VLookup(CLng(Mid(TextBox1.Text, 6, 5)), Range("AV1:AW36529"), 2, False) 
     if IsError(CommNaiss) then msgbox("value not found") 
    On Error Goto 0 ' remember to turn on error resume next off again 
0

無需添加On-Error-GoTo因爲VLookup函數沒有拋出錯誤,但它返回它。嘗試聲明變量DptVariant,並檢查IsError如果VLookup返回錯誤。

Sub test() 
    Dim Dpt As Variant 
    Dpt = Application.VLookup("searched-text", Range("A1:C3"), 2, False) 
    If IsError(Dpt) Then 
     MsgBox "Error '" & DecodeError(Dpt) & "' occured.", vbCritical 
    End If 
End Sub 

這裏是一個函數的例子,它將解碼由VLookup返回的錯誤編號以描述字符串。

Private Function DecodeError(ByVal error As Variant) As String 
    On Error Resume Next 
    Select Case CLng(error) 
     Case xlErrDiv0 
      DecodeError = "#DIV/0!" 
     Case xlErrNA 
      DecodeError = "#N/A" 
     Case xlErrName 
      DecodeError = "#NAME?" 
     Case xlErrNull 
      DecodeError = "#NULL!" 
     Case xlErrNum 
      DecodeError = "#NUM!" 
     Case xlErrRef 
      DecodeError = "#REF!" 
     Case xlErrValue 
      DecodeError = "#VALUE!" 
     Case Else 
      DecodeError = "Unknown error" 
    End Select 
End Function 
+0

謝謝你。我會盡快嘗試。我現在沒有更多的錯誤,但無論如何,我需要改進這個代碼,我只用宏開始。我接受你的建議。讓我學習更多的好方法。 – Ronan