Excel VBA-UDF返回0或为空或#value

时间:2019-06-10 09:19:34

标签: excel vba user-defined-functions

我正在创建我的自定义功能。我编写了代码并将其测试为“ sub,并且效果很好。然后,我将其转换为可以在一般情况下使用的功能。我改变的是添加函数声明,从excel单元格获取输入并指定函数输出。其他所有都保持不变。

我的函数只有一个输入,这是从Excel工作表中选择的单元格。我希望该函数返回一个输出。但是,它返回0。 •函数声明。 “功能IbpBomLevel(ByVal目标作为范围)作为变体 •输入功能作为选定单元格。 “产品ID =目标 •功能输出。 “ IbpBomLevel = fullText

我使用显式选项来避免不存在的功能。另外,我确信输入,函数确实将所选单元格作为输入。但是问题在于,必须在每个循环中更改“ ProductID。但是,当我声明“ IbpBomLevel(函数的输出)= ProductID并看到ProductID是用户从单元格中选择的第一个参数时。这意味着循环不起作用。当我将其测试为” sub”时,得到的结果是我想要。我不确定是什么问题。

Option Explicit

Function IbpBomLevel(ByVal Target As Range) As Variant

Dim Wb As Workbook
Dim Ws As Worksheet
Dim MyRange As Range
Dim SourceID As Variant
Dim SourceID2 As Variant
Dim SourceID3 As Variant
Dim Product As Variant
Dim Item As Variant
Dim Location As Variant
Dim Resource As Variant
Dim I As Variant
Dim T As Variant
Dim Z As Variant
Dim X As Variant
Dim Y As Variant
Dim Index As Variant
Dim Index2 As Variant
Dim Index3 As Variant
Dim BomLevel As Variant
Dim FoundCell As Variant
Dim fullText As Variant

Dim ProductID As Variant
ProductID = Target

Set Wb = Workbooks("Kitap.xlsx")
Windows("Kitap.xlsx").Activate

On Error GoTo T_Error

Set Ws = Wb.Worksheets("Production Source Header")
Sheets("Production Source Header").Select

Set MyRange = Worksheets("Production Source Header").Range("B:C")

SourceID = CVar(Application.WorksheetFunction.VLookup(ProductID, MyRange, 2, False))

I = 1
T = 0
Z = 1

If IsEmpty(SourceID) = False Then

    Do While (IsEmpty(SourceID) = False) And (T = 0)

        BomLevel = Z

        Windows("Kitap.xlsx").Activate
        Set Ws = Wb.Worksheets("Production Source Header")
        Sheets("Production Source Header").Select
        Set MyRange = Worksheets("Production Source Header").Range("B:C")

        SourceID = CVar(Application.WorksheetFunction.VLookup(ProductID, MyRange, 2, False))

        Set FoundCell = ActiveSheet.Range("C:C").Find(What:=SourceID)

        If Not FoundCell Is Nothing Then
            Index = FoundCell.Row
            Location = Cells(Index, 1)
            Product = Cells(Index, 2)
        Else
        End If

        X = I
        I = I + 1

        Windows("Kitap.xlsx").Activate
        Set Ws = Wb.Worksheets("Production Source Item")
        Sheets("Production Source Item").Select
        Set MyRange = Worksheets("Production Source Item").Range("B:B")

        SourceID2 = CVar(Application.WorksheetFunction.VLookup(SourceID, MyRange, 1, False))

        Do While (IsEmpty(SourceID2) = False) And (I - X = 1)

            Set MyRange = Worksheets("Production Source Item").Range("B:B")

            SourceID2 = CVar(Application.WorksheetFunction.VLookup(SourceID, MyRange, 1, False))

            Set FoundCell = ActiveSheet.Range("B:B").Find(What:=SourceID2)

            If Not FoundCell Is Nothing Then
                Index2 = FoundCell.Row
                Item = Cells(Index2, 1)
                Windows("Kitap.xlsx").Activate
                Set Ws = Wb.Worksheets("Production Source Header")
                Sheets("Production Source Header").Select
            Else
            End If

            Y = I
            I = I + 1

            Windows("Kitap.xlsx").Activate
            Set Ws = Wb.Worksheets("Production Source Resource")
            Sheets("Production Source Resource").Select

            Set MyRange = Worksheets("Production Source Resource").Range("B:B")

            SourceID3 = CVar(Application.WorksheetFunction.VLookup(SourceID, MyRange, 1, False))

            Do While IsEmpty(SourceID3) = False And (I - Y = 1)

                Set MyRange = Range("B:B")
                    SourceID3 = CVar(Application.WorksheetFunction.VLookup(SourceID, MyRange, 1, False))
                Set FoundCell = ActiveSheet.Range("B:B").Find(What:=SourceID3)

                If Not FoundCell Is Nothing Then
                    Index3 = FoundCell.Row
                    Resource = Cells(Index3, 1)
                    Windows("Kitap.xlsx").Activate
                    Set Ws = Wb.Worksheets("Production Source Header")
                    Sheets("Production Source Header").Select
                Else
                End If

            I = I + 1

            Loop
        Loop

        fullText = fullText & " Location: " & Location & " // Header: " & Product & " // Item: " & Item & " // Resource: " & Resource
        Z = Z + 1

        ProductID = Item
        Set MyRange = Worksheets("Production Source Header").Range("B:C")

        SourceID = (Application.WorksheetFunction.VLookup(ProductID, MyRange, 2, False))

T_Error:
        If Err.Number = 1004 Then

            On Error Resume Next
            T = 1

        Else
        End If

    Loop

    IbpBomLevel = fullText

Else
    MsgBox ("Bom Missing")
End If

End Function

0 个答案:

没有答案
相关问题