2016-04-07 23 views
1

マクロで発生したエラーを処理する方法がわかりません。Excelでエラーが発生した場合のハンドル

を介して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.... 
+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

は、ここでは2つの方法

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 - メートルの答え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

VLookup関数がエラーをスローしませんが、それはそれを返すためOn-Error-GoToを追加する必要はありません。変数DptVariantと宣言し、VLookupがエラーを返した場合はIsErrorにチェックを入れてください。

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

関連する問題