Search code examples
vbaexcelexcel-match

Application.Match mismatch error 13 - How to resolve?


I have been working on some code that goes through and tries to find "New Muted Price" or "New Opposed Price". If that is not found during the array iteration, it comes up with a

Mismatch error run code 13

I know this is because it did not find it, but how do I just get it to skip past the error and continue with the code? I have tried

if not iserror (PriceCol = Application.Match("New Opposed Price", rng, 0)) then but it is still showing the mismatch error. 

The portions of the code where the error comes up is:

 PriceCol = Application.Match("New Opposed Price", rng, 0)

and

pricecol2 = Application.Match("New Muted Price", rng, 0)

Does anyone have some advise on how to resolve this issue?

Sub WIP()
    Dim wb As Workbook
    Dim wsMain As Worksheet
    Dim wsLookup As Worksheet
    Dim rng As Range
    Dim rng2 As Range
    Dim rFind1 As Range
    Dim rFind2 As Range
    Dim rFind3 As Range
    Dim MyArray As Variant
    Dim LookupHeaders As Variant
    Dim LookupHeaders2 As Variant
    Dim LR As Long
    Dim i As Long
    Dim PriceCol As Long
    Dim pricecol2 As Long
    Dim LastColumn As Long
     Dim LastColumn2 As Long
       Dim LastColumn3 As Long
     Dim LastColumn4 As Long
    Dim IndexCol As Long

     'Unformatted Price Row
  Sheets("Consolidate List").Select
    LR = Range("A" & Rows.Count).End(xlUp).Row
    Columns("H:H").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    Columns("J:N").Delete
    Columns("J:J").Select
    ActiveWindow.FreezePanes = True
    Range("H2").Select
    ActiveCell.FormulaR1C1 = "New Price"
    ActiveCell.Interior.ColorIndex = 22
         Range("H3:H" & LR).Formula = "=VLOOKUP(RC[-7],'Connect Report'!C[-7]:C[-6],2,FALSE)"
         ActiveCell.EntireColumn.Resize(Rows.Count - 2).Offset(2).Select
 Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
        Range("I2").Select
    ActiveCell.FormulaR1C1 = "Difference"
    ActiveCell.Interior.ColorIndex = 22
    Range("I3:I" & LR).Formula = "=IF(OR(OR(RC[-2]="""",RC[-1]="""",RC[-1]=""x"",)),"""",RC[-1]-RC[-2])"
         ActiveCell.EntireColumn.Resize(Rows.Count - 2).Offset(2).Select
 Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False

    Set wb = ActiveWorkbook
     Sheets("Consolidate List").Select
    Set wsMain = wb.ActiveSheet
    Set wsLookup = wb.Sheets("Connect Report")     '<-- Change to correct sheet name for the Lookup sheet
    LR = wsMain.Cells(wsMain.Rows.Count, "A").End(xlUp).Row
    MyArray = Array("US", "SPAIN", "California")
    LookupHeaders = Array("TTIER", "Time333", "Round6")
  LookupHeaders2 = Array("TELLER5", "Fly7", "Mine4")

    For i = LBound(MyArray) To UBound(MyArray)
        With wsMain.Rows(1)
            Set rFind1 = .Find(What:=MyArray(i), LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False)
            If Not rFind1 Is Nothing Then
                Set rng = rFind1.Offset(1).Resize(, 8)
                PriceCol = Application.Match("New Opposed Price", rng, 0)
                LastColumn = rFind1.Column + PriceCol
                If wsMain.Cells(rng.Row, LastColumn) <> "New Opposed Price" Then
                    wsMain.Columns(LastColumn).Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
                    wsMain.Cells(rng.Row, LastColumn).Value = "New Opposed Price"
                    wsMain.Cells(rng.Row, LastColumn).Interior.ColorIndex = 22
                    LastColumn2 = LastColumn + 1
                     wsMain.Columns(LastColumn2).Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
                    wsMain.Cells(rng.Row, LastColumn2).Value = "Difference"
                    wsMain.Cells(rng.Row, LastColumn2).Interior.ColorIndex = 22

                Set rFind2 = wsLookup.Rows(1).Find(LookupHeaders(i), wsLookup.Range("A1"), xlValues, xlWhole)
                If Not rFind2 Is Nothing Then
                    IndexCol = rFind2.Column
                    wsMain.Cells(rng.Row + 1, LastColumn).Resize(LR - 2).Formula = "=VLOOKUP(A" & rng.Row + 1 & ",'Connect Report'!$A:$AL," & IndexCol & ",FALSE)"

                      wsMain.Cells(rng.Row + 1, LastColumn2).Resize(LR - 2).Formula = "=IF(OR(OR(RC[-2]="""",RC[-1]="""",RC[-1]=""x"",)),"""",RC[-1]-RC[-2])"
                              Else
                    MsgBox "Excel could not find " & LookupHeaders(i) & " in the lookup table."
                End If

                Set rng2 = rFind1.Offset(1).Resize(, 8)
                pricecol2 = Application.Match("New Muted Price", rng, 0)
                LastColumn3 = rFind1.Column + pricecol2
                   If wsMain.Cells(rng.Row, LastColumn3) <> "New Muted Price" Then
                    wsMain.Columns(LastColumn3).Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
                    wsMain.Cells(rng2.Row, LastColumn3).Value = "New Muted Price"
                    wsMain.Cells(rng2.Row, LastColumn3).Interior.ColorIndex = 22
                    LastColumn4 = LastColumn3 + 1
                      wsMain.Columns(LastColumn4).Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
                    wsMain.Cells(rng2.Row, LastColumn4).Value = "Difference"
                    wsMain.Cells(rng2.Row, LastColumn4).Interior.ColorIndex = 22
                End If

                      Set rFind3 = wsLookup.Rows(1).Find(LookupHeaders2(i), wsLookup.Range("A1"), xlValues, xlWhole)
                If Not rFind3 Is Nothing Then
                    IndexCol = rFind3.Column
                    wsMain.Cells(rng2.Row + 1, LastColumn3).Resize(LR - 2).Formula = "=VLOOKUP(A" & rng2.Row + 1 & ",'Connect Report'!$A:$AL," & IndexCol & ",FALSE)"

                      wsMain.Cells(rng2.Row + 1, LastColumn4).Resize(LR - 2).Formula = "=IF(OR(OR(RC[-2]="""",RC[-1]="""",RC[-1]=""x"",)),"""",RC[-1]-RC[-2])"

                    Else
                    MsgBox "Excel could not find " & LookupHeaders2(i) & " in the lookup table."
                End If
                End If
             End If
        End With
    Next i
End Sub

Solution

  • You're on the right track using Application.Match (which can return an Error object) versus WorksheetFunction.Match (which always raises the error).

    But since your PriceCol and PriceCol2 variables are strongly typed as Long, you'll get a Mismatch error.

    Use another throwaway variable to handle the return:

    Dim matchVal as Variant
    matchVal = Application.Match("New Muted Price", rng, 0)
    If Not IsError(matchVal) Then
        PriceCol2 = matchVal
        ...
    
    
    Else
        ' if there is no match, you may need to do something else here.
    
    End If
    

    Alternatively, you could use the Range.Find method:

    If Not rng.Find("New Muted Price") Is Nothing
        PriceCol2 = Application.Match("New Muted Price", rng, 0)
    Else
        ...