“Invalid Procedure Call or Argument” when using Application.Match within VBA Function

Deadly 提交于 2021-01-29 08:51:07

问题


This is my function:

Function stageValueVariance(stage As String, valCol As Long)
    For i = 2 To offlineHeight
        If Application.VLookup(offline.ListColumns(1).Range(i).value, bce.DataBodyRange, valCol, 0) <> offline.ListColumns(valCol).Range(i).value Then
            foundID = Application.WorksheetFunction.Match(offline.ListColumns(1).Range(i), oldOut.ListColumns(1).DataBodyRange, 0)
            If foundID = 0 Then
                foundID = Application.WorksheetFunction.Match(offline.ListColumns(1).Range(i), valComp.ListColumns(1).DataBodyRange, 0)
            End If
            If foundID = 0 Then
                With stageValComp.ListRows.Add
                    .Range(1) = offline.ListColumns(1).Range(i)
                    .Range(2) = offline.ListColumns(2).Range(i)
                    .Range(3) = stage
                    .Range(4) = offline.ListColumns(7).Range(i)
                    .Range(5) = Application.VLookup(offline.ListColumns(1).Range(i).value, bce.DataBodyRange, valCol, 0)
                    .Range(6).Validation.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Formula1:="Yes, No"
                End With
            End If
            foundID = 0
        End If
    Next i
End Function

I'm getting the 'Invalid Procedure Call or Argument' error on this line:

foundID = Application.WorksheetFunction.Match(offline.ListColumns(1).Range(i), oldOut.ListColumns(1).DataBodyRange, 0)

If it's useful, that line of code is checking that an ID doesn't already exist within another table, so offline and oldOut are declared as ListObjects at the top of the module.

I've never come across this error before, and can't seem to find much information online. Any ideas?


回答1:


You will probably find the DataBodyRange in oldOut.ListColumns(1).DataBodyRange doesn't exist.

If testing oldOut.ListColumns(1).DataBodyRange.Address returns an object not set error then this is definitely the problem.

The only way I can reproduce your error is by deleting the DataBodyRange or by deleting the table. Code works fine otherwise.



来源:https://stackoverflow.com/questions/60685013/invalid-procedure-call-or-argument-when-using-application-match-within-vba-fun

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