我无法弄清楚如何处理宏中出现的错误。
通过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 :(得分:5)
我会使用GoTo ErrorHandler:
,拥有MsgBox
,然后重新开始。
On Error GoTo ErrorHandler
ErrorHandler:
MsgBox "Value does not exist"
Resume Next
答案 1 :(得分:2)
蒂姆的回答 - 使用错误处理程序是最好的,但如果你想在接下来的错误恢复上使用,那么你可以使用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
答案 2 :(得分:1)
这里有两种可能的方式
1)&#34; On Error ...&#34;方式
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)&#34;发现&#34;方式
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
答案 3 :(得分:0)
这有什么好处? -
CommNaiss = Application.WorksheetFunction.IfError( _
Application.WorksheetFunction.VLookup(CLng(Mid(TextBox1.Text, 6, 5)) _
, Range("AV1:AW36529"), 2, False), "Error")
答案 4 :(得分:0)
无需添加answer1 = parseInt(prompt(question1.q1+" "+" 1. " +question1.a1+" "+" 2. "+question1.a2+" "+" 3. "+question1.a3));
while(answer1 < 1 || answer1 > 3){
answer1 = parseInt(prompt("Please enter a number between 1 and 3: "+question1.q1+ " "+" 1. "+question1.a1+" "+" 2. "+question1.a2+" "+" 3. "+question1.a3 ));
}
,因为On-Error-GoTo
函数不会抛出错误但会返回错误。尝试将变量VLookup
声明为Dpt
,如果Variant
返回错误,请与IsError
核对。
VLookup
这是一个函数示例,它将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