UDF Vlookup总和错误。始终返回“ #VALUE!”

时间:2019-04-26 12:07:28

标签: excel vba

我想创建一个函数,该函数返回Vlookup的所有返回值的总和。查找值的参数用“&”分隔。另外,必须在参数之前添加“ DD”的“ DD”,“ DD0”或“ DD00”,以匹配查找范围内的查找值。例如,Cell(1,2)=“ DD003&03A”,该函数应将DD003和DD003A的返回值相加。以下是我的代码:

Option Explicit

Public Function DPPR_Vlookup(Lookup_Value As String, Lookup_Range As Range, Column_Index As Long, Optional Match_Case As Integer) As Variant

Dim D_Code() As String

Dim Pos_1 As Long

Dim i As Integer

Dim Sum_Value As Long

Dim x1 As Long
Dim x2 As Long
Dim x3 As Long


Pos_1 = InStr(1, Lookup_Value, "&", vbTextCompare)

    With Application.WorksheetFunction

        If Pos_1 = 0 Then

        DPPR_Vlookup = .VLookup(Lookup_Value, Lookup_Range, Column_Index, Match_Case)

        Else

        D_Code() = Split(Lookup_Value, "&")

            Sum_Value = 0

            For i = 1 To UBound(D_Code())

                If IsError(.VLookup("DD" & D_Code(i), Lookup_Range, Column_Index, Match_Case)) = True Then

                x1 = 0

                Else

                x1 = .VLookup("DD" & D_Code(i), Lookup_Range, Column_Index, Match_Case)

                End If


                If IsError(.VLookup("DD0" & D_Code(i), Lookup_Range, Column_Index, Match_Case)) = True Then

                x2 = 0

                Else

                x2 = .VLookup("DD0" & D_Code(i), Lookup_Range, Column_Index, Match_Case)

                End If


                If IsError(.VLookup("DD00" & D_Code(i), Lookup_Range, Column_Index, Match_Case)) = True Then

                x3 = 0

                Else

                x3 = .VLookup("DD00" & D_Code(i), Lookup_Range, Column_Index, Match_Case)

                End If


                Sum_Value = Sum_Value + x1 + x2 + x3

                x1 = 0
                x2 = 0
                x3 = 0

            Next i


DPPR_Vlookup = Sum_Value


        End If

    End With

End Function



所有带有“&”的参数的结果始终为“ #VALUE!”。我已经考虑到错误,该值应该为0。此外,lookup_range中同时存在DD003和DD003A的值,但该函数仅返回“ #VALUE!”。请帮忙。

1 个答案:

答案 0 :(得分:0)

请注意,对于DD003&03A的第一个值,您无需添加前缀。同样,Split方法返回的数组索引以0开头,因此,您需要以i = 0开头,甚至最好以i = LBound(D_Code())动态地开头。

如评论WorksheetFunction.Vlookup中所述,返回的运行时错误无法用IsError()捕获,因此我将其更改为Application.Vlookup

我还为可能的前缀添加了一个数组,因此我们可以轻松地使用循环,而不必一遍又一遍地重复相同的代码。

最后使用测试过程轻松调试功能,因此您可以使用F8逐步进行测试。

Option Explicit

Public Sub Test_DPPR_Vlookup()

    Debug.Print DPPR_Vlookup([E2], [A:B], 2, 0)

End Sub

Public Function DPPR_Vlookup(Lookup_Value As String, Lookup_Range As Variant, Column_Index As Variant, Optional Match_Case As Boolean) As Variant
    Dim Pos_1 As Long
    Pos_1 = InStr(1, Lookup_Value, "&", vbTextCompare)

    Dim Prefix As Variant
    Dim Prefixes() As Variant
    Prefixes = Array("", "DD", "DD0", "DD00") 'note that the first is needed to also lookup without prefixes (first item)

    With Application
        If Pos_1 = 0 Then
            DPPR_Vlookup = .VLookup(Lookup_Value, Lookup_Range, Column_Index, Match_Case)
        Else
            Dim D_Code() As String
            D_Code() = Split(Lookup_Value, "&")

            Dim Sum_Value As Long
            Sum_Value = 0

            Dim i As Long, LookupResult As Variant
            For i = 0 To UBound(D_Code()) 'note array item counting starts with 0
                For Each Prefix In Prefixes
                    LookupResult = .VLookup(Prefix & D_Code(i), Lookup_Range, Column_Index, Match_Case)
                    If Not IsError(LookupResult) Then
                        Sum_Value = Sum_Value + LookupResult
                    End If
                Next Prefix
            Next i

            DPPR_Vlookup = Sum_Value
        End If
    End With
End Function

用于Result的公式是=DPPR_Vlookup(E2,A:B,2,0)

enter image description here