简体   繁体   中英

VBA Excel Error Handling Mismatch type 13

It's my first time writing a code that actually attempts to do something and I'm having some trouble with writing an error handler. The code attempts to match 'tosearch' to the range I1:I10. If it finds a match and the cell is blank is asks for user input.

Obviously there is a problem in matching with loops as the loop will break at the slightest sniff of a mismatch. I attempted to solve this with the 'If iserror(etc.)' but I still get mismatch error type 13 and the debugger points me to this line

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

Please ignore the horrendous, sprawling mess beneath. It works (just) and while it doesn't have any practical use yet, i hope to use it to quickly identify matches between arrays but also have some control over whether the 'correct' match is displaying the right information.

If anyone has any answers your help would be hugely appreciated.

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

I think the problem may be caused because there is a difference between the data types in your range and the string variable toSearch. I'd recommend settting toSearch as follows:

Dim tosearch As Variant

The following link describes a similar problem and a similar solution to the one I have defined here.

http://www.excelforum.com/excel-programming-vba-macros/358571-application-match.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM