Excel VBA 函数 B 未显示被调用函数 A 的结果

Excel VBA function B not showing result of called function A

我正在改进我的 Excel sheet 以跟踪我分享的威士忌桶,但我 运行 遇到了一个奇怪的问题,在至少对我来说很奇怪。

我创建的 caskinfoBySKU 函数有效,基本上是一个 INDEX MATCH(row) MATCH(column) 函数。

第二个函数 caskinfoBySKU_DYN 也可以工作,但无法显示它应该从 caskinfoBySKU 函数获得的结果。 caskinfoBySKU_DYN 根据付款类型(初始与最终)确定哪些列名需要与 caskinfoBySKU 函数一起使用。

我不是经常在 VBA 中编程,在寻找解决方案一周后,我目前陷入了让这个工作正常的过程中。

有人看到我哪里出错了吗?

我确实怀疑问题出在我调用原始函数的方式上......我已经尝试了几种在搜索过程中发现的方法,但不幸的是没有成功让它工作:/

我用下面的 VBA 代码和一些测试数据创建了我的 Excel 文档的修剪副本。可以从我的Google Drive.

下载

(当然,当您发现使 VBA 代码更好/更快的方法时,请随时向我指出。)

Public Function caskinfoBySKU(SKU, colname) As Variant

    On Error GoTo CleanFail

    Dim indexResult As Variant
    Dim rowMatch, colMatch As Long
    
    SKU = Trim(SKU)
    colname = Trim(colname)
    
    If SKU = "" Or colname = "" Then
        GoTo CleanFail
    End If
           
    'in which row do we find the SKU
    rowMatch = WorksheetFunction.Match(SKU, Range("tbl_caskinfo[SKU]"), 0)

    'is rowMatch an integer?
    If Int(rowMatch) = rowMatch Then
        
        'in which column do we find the colname
        colMatch = WorksheetFunction.Match(colname, Range("tbl_caskinfo[#headers]"), 0)
    
        'is colMatch an integer?
        If Int(colMatch) = colMatch Then
            'get result
            indexResult = WorksheetFunction.Index(Range("tbl_caskinfo"), rowMatch, colMatch)

            If Not indexResult = "" Then
                GoTo ReturnResult
            Else 'no result found
                GoTo CleanFail
            End If
        
        Else 'colname not found in colnames row
            GoTo CleanFail
        End If
    
    Else 'SKU not found in SKU column
        GoTo CleanFail
    End If

CleanFail:
    caskinfoBySKU = "•" 'ALT+0149
    Exit Function

ReturnResult:
    'MsgBox rowMatch & "," & colMatch & ":   " & indexResult
    caskinfoBySKU = indexResult
End Function

Public Function caskinfoBySKU_DYN(key, payment, SKU) As Variant
' redirect to Est. Yield / Act. Yield column or Initial Payment / Act. Final Payment
    On Error GoTo CleanFail
    
    Dim colname As Variant
    
    payment = Trim(payment)
    
    If payment = "" Then
        GoTo CleanFail
    End If
    
    Select Case UCase(key)
        Case "YIELD"
            Select Case LCase(payment)
                Case "initial"
                    colname = "Est. Yield"
                    GoTo CallParent
                Case "final"
                    colname = "Act. Yield"
                    GoTo CallParent
                Case Else
                    GoTo CleanFail
            End Select
    
        Case "UNIT PRICE", "PRICE"
            Select Case LCase(payment)
                Case "initial"
                    colname = "Initial Payment"
                    GoTo CallParent
                Case "final"
                    colname = "Act. Final Payment"
                    GoTo CallParent
                Case Else
                    GoTo CleanFail
            End Select
    
        Case Else
            GoTo CleanFail
    End Select
    
CleanFail:
    caskinfoBySKU_DYN = "•" 'ALT+0149
    Exit Function
    
CallParent:
    Call caskinfoBySKU(SKU, colname)
End Function

caskinfoBySKU_DYN = caskinfoBySKU(SKU, colname) 而不是 Call caskinfoBySKU(SKU, colname) 成功了。

感谢CDP1802