2016-04-07 91 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