Excel VBA Run-time error '13': Type mismatch

时光总嘲笑我的痴心妄想 提交于 2020-01-21 15:07:24

问题


I'm not great at VBA, but all of a sudden, I started getting this error. Please let me know if you can find what the error is. I'm so stuck on this one. I did search everywhere and found lots of similar posts but the solution always seems to be very specific to the code. Any of you experts would be greatly appreciated.

Here's my code:

Sub FindSimilar()
    Dim phrases As Range, phrase As Range
    Dim terms As Range, term As Range
    Dim matches As String
    Dim words() As String

    'ensure this has the correct sheet names for your workbook
    Set phrases = ThisWorkbook.Worksheets("Export").Range("B2:B3500")
    Set terms = ThisWorkbook.Worksheets("Topics").Range("D767:D967")

    For Each term In terms
        matches = ""
        words() = Split(term.Value)

        For i = 0 To UBound(words, 1)
        If Len(words(i)) > 2 Then
         Select Case words(i)
          Case "examplewords", "blacklist"
          Case Else
            For Each phrase In phrases
                If InStr(1, phrase.Value, words(i)) Then
                    matches = matches & phrase & "/"
                End If
            Next phrase
         End Select
        End If

I'm really at a loss as to why the

Run-time error 13: Type mismatch

is being thrown.

The breakpoint occurs at the following lines:

        matches = ""

And

       Set terms = ThisWorkbook.Worksheets("Topics").Range("D773:D779")

And

       For i = 0 To UBound(words, 1)

回答1:


A Run-time error '13': Type mismatch halfway through looping through a large set of values collected from the worksheet is almost always due to encountering a worksheet error code.

Are there any worksheet error codes (e.g. #N/A, #VALUE!, etc) in either Export!B2:B3500 or Topics!D767:D967 ?

I found one single #N/A cell. It was not there as an error but as a pasted value :-( It's working now!

You can quickly locate any worksheet errors in a long column of values (or even an entire worksheet) with these steps.

  1. Select entire column. If you want to look at the whole worksheet, just select any single cell. 2, Tap F5 then click Special.
  2. Choose Formulas and leave only Errors checked.
  3. Click OK.

Worksheet errors can also be found with one or both of the following.

<range>.SpecialCells(xlCellTypeConstants, xlErrors)
<range>.SpecialCells(xlCellTypeFormulas, xlErrors)


来源:https://stackoverflow.com/questions/48957607/excel-vba-run-time-error-13-type-mismatch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!