错误13 - 类型不匹配 - 索引匹配

时间:2018-03-14 16:58:58

标签: excel vba excel-vba

在索引/匹配上获得上述错误。将尝试保持这个简短而甜蜜,但我是一个VBA菜鸟。所有被调用的东西都有数据。我注意到的一件事是,当我进行调试打印时,RefCol(一系列数字)具有前导和尾随空格。但是,当我测试值的长度时,它返回了正确的值。

我无法理解是什么打破了它,我在工作簿中做了一个索引匹配,它运行得很好。

Private Sub Ref_Change()

Dim ws As Worksheet
Dim tbl As ListObject

Set ws = Worksheets("Details")
Set tbl = ws.ListObjects("Call_Log")

Dim RefCol As Range
Dim NameCol As Range
Dim PhoneCol As Range
Dim DateCol As Range

Set RefCol = tbl.ListColumns("Ref Number").DataBodyRange
Set NameCol = tbl.ListColumns("Caller Name").DataBodyRange
Set PhoneCol = tbl.ListColumns("Telephone").DataBodyRange
Set DateCol = tbl.ListColumns("Date").DataBodyRange

Me.CallDate.Value = Application.WorksheetFunction.Index(DateCol, Application.Match(Me.Ref.Value, RefCol, 0))

End Sub

我是否正确设置了这个?

由于

埃文

1 个答案:

答案 0 :(得分:0)

如上所述,很可能未找到匹配,并且错误正在传递给INDEX。

拉出MATCH并测试错误,然后在数据中找到正确的单元格。

Private Sub Ref_Change()

Dim ws As Worksheet
Dim tbl As ListObject

Set ws = Worksheets("Details")
Set tbl = ws.ListObjects("Call_Log")

Dim RefCol As Range
Dim NameCol As Range
Dim PhoneCol As Range
Dim DateCol As Range

Set RefCol = tbl.ListColumns("Ref Number").DataBodyRange
Set NameCol = tbl.ListColumns("Caller Name").DataBodyRange
Set PhoneCol = tbl.ListColumns("Telephone").DataBodyRange
Set DateCol = tbl.ListColumns("Date").DataBodyRange

Dim mtchRow As Long
mtchRow = 0
On Error Resume Next
    mtchRow = Application.WorksheetFunction.Match(Me.ref.Value, RefCol, 0)
On Error GoTo 0
If mtchRow > 0 Then
    Me.CallDate.Value = DateCol.Cells(mtchRow, 1).Value
Else
    MsgBox "'" & Me.ref.Value & "' not found, or lookup array is more than one column or row"
End If



End Sub