2014-03-27 99 views
0

這是我第一次編寫實際嘗試做某些事情的代碼,並且在編寫錯誤處理程序時遇到了一些麻煩。該代碼嘗試將「tosearch」與範圍I1:I10相匹配。如果它找到匹配並且單元格爲空,則要求用戶輸入。VBA Excel錯誤處理不匹配類型13

很明顯,在與循環匹配時存在一個問題,因爲循環會在最輕微的不匹配中斷。我試圖用解決這個「如果ISERROR(等)。」但是我還是不匹配的錯誤類型13和調試器指向我此行

If IsError(myvalue = Application.Match(tosearch, Range("i1:i10"), 0)) Then 

請忽略下面可怕的,廣闊的一塌糊塗。它可以正常工作,雖然它還沒有任何實際用途,但我希望能夠使用它快速識別數組之間的匹配,但也能夠控制「正確」匹配是否顯示正確的信息。

如果有人有任何答案,您的幫助將非常感激。

Dim myvalue As String 
Dim myrng As Long 
Dim tosearch As String 
Dim myrnglimit As Long 
Dim Uchoose As String 
Dim animal As Variant 
Dim blankchck As String 

myrnglimit = 15 

For myrng = 2 To myrnglimit 

    'isblank check 
blankchck = Range("c" & myrng).Value 
    If blankchck = "" Then 

'sets tosearch as each cell 
tosearch = Range("a" & myrng).Value 

    'error checker then it matches each cell to the table hardcoded into the code 
If IsError(myvalue = Application.Match(tosearch, Range("i1:i10"), 0)) Then 
GoTo nextloop: 
Else 

     myvalue = Application.Match(tosearch, Range("i1:i10"), 0) 

    'fills in the second column with Animal data 
     animal = Range("j" & myvalue).Value 
     Range("a" & myrng).Offset(0, 1).Value = animal 

'User input for animal 
Uchoose = MsgBox("Excel says : " & Range("k" & myvalue).Value & ". So are " & animal & "     good?", vbYesNoCancel, "Status") 

If Uchoose = vbYes Then 
Range("a" & myrng).Offset(0, 2).Value = "Good" 
    ElseIf Uchoose = vbNo Then 
     Range("a" & myrng).Offset(0, 2).Value = "Bad" 
    ElseIf Uchoose = vbCancel Then 
     GoTo nextloop: 
End If 


'Select case to identify 1004 mismatch and skip 
nextloop: 

'Error checker if 
End If 
'Avoidblank if 
End If 

Next myrng 
+0

我不明白你的問題究竟是什麼? – 2014-03-27 11:20:24

+0

你應該使用'If IsError(Application.Match(tosearch,Range(「i1:i10」),0))然後'如果沒有錯誤,將結果寫入變量 –

+0

這樣做了,我不小心就離開了在錯誤捕捉器的'myvalue ='中,謝謝。 – user3468246

回答