简体   繁体   中英

Match function error excel vba

Hey I am trying to use the or statement to execute a certain set of conditions which will decide whether the function will execute the Index(match) property or not. But when there is a combobox(form control) with myindex = 1, It executes the code correctly but also gives an error saying "Unable to get match property.." can someone help ?

Option Explicit
Dim ws As Sheets
Dim i As Integer, j As Integer, p As Integer, q As Integer
Dim myindex As Long, myitem As String


Public Function FEC(i, j)

 Set ws = Sheets(Array("S1 Fuel Consumption", "EF_Stat", "Summary"))
 myindex = ws(1).Shapes("Fuel " & j).ControlFormat.Value

 If myindex = 2 Or 3 Or 4 Then

   With Application.WorksheetFunction

     FEC = .Index(ws(2).Range("B2:D5"), .Match(ws(1).Range("A" & i), ws(2).Range("A2:A5"), 0), .Match(ws(1).Shapes("Fuel " & j).ControlFormat.List(ws(1).Shapes("Fuel " & j).ControlFormat.ListIndex), ws(2).Range("B1:D1"), 0))

   End With

     Else
        FEC = 0

 End If
 End Function

 Sub Xecute()
   Set ws = Sheets(Array("S1 Fuel Consumption", "EF_Stat", "Summary"))

 For i = 7 To 15 Step 4
    j = i - 6
     Do While j < i - 2
        ws(3).Range("D" & j).Value = FEC(i, j)
        j = j + 1
    Loop
 Next i

End Sub

Your code is very complicated to debug because there is so much happening on one line. We can't tell which part is causing the problem. So I suggest you break it out so you can see what is happening.

Replace this:

With Application.WorksheetFunction
    FEC = .Index(ws(2).Range("B2:D5"), .Match(ws(1).Range("A" & i), ws(2).Range("A2:A5"), 0), .Match(ws(1).Shapes("Fuel " & j).ControlFormat.List(ws(1).Shapes("Fuel " & j).ControlFormat.ListIndex), ws(2).Range("B1:D1"), 0))
End With

With this:

With Application.WorksheetFunction

    Dim m1LookupValue As Variant
    Dim m2LookupValue As Variant
    Dim m1 As Variant
    Dim m2 As Variant

    m1LookupValue = ws(1).Range("A" & i)
    m2LookupValue = ws(1).Shapes("Fuel " & j).ControlFormat.List( _
                        ws(1).Shapes("Fuel " & j).ControlFormat.ListIndex)

    Debug.Print m1LookupValue
    Debug.Print m2LookupValue

    m1 = .Match(m1LookupValue, ws(2).Range("A2:A5"), 0)
    m2 = .Match(m2LookupValue, ws(2).Range("B1:D1"), 0)

    Debug.Print m1
    Debug.Print m2

    FEC = .Index(ws(2).Range("B2:D5"), m1, m2)

End With

Now you can use F8 to step through your code. Look carefully at the lookup values and make sure they are within limits for the match function. Then look carefully at the m1 and m2 to make sure they are within limits for the index function.

Using this method makes the code easier to read and much easier to debug. You will still get the error but now you can see exactly where the error is. And hopefully you will be able to fix it yourself. If not, let me know where the problem is and what the value of the variables are.

Also be sure to make the change suggested by Loannis: myindex = 2 Or myindex = 3 Or myindex = 4

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