Search code examples
excelvbacall

Excel VBA function B not showing result of called function A


I'm improving my Excel sheet to keep track of the whisky casks I've shares in but I'm running into a bit of a weird issue, at least for me it's weird.

The caskinfoBySKU function that I've created works and is basically an INDEX MATCH(row) MATCH(column) function.

The second function, caskinfoBySKU_DYN, also works BUT fails at showing the result it should get from the caskinfoBySKU function. The caskinfoBySKU_DYN sorts out which column name needs to be used with the caskinfoBySKU function for some columns which depend on the payment type (initial vs final).

I'm not programming in VBA that often and after a week of searching for a solution I'm currently stuck in my quest to get this working.

Does anyone see where I go wrong?

I do suspect the problem is with how I call the original function... I've tried several ways I've found during my searches but unfortunately without any success in getting it to work :/

I created a trimmed copy of my Excel document with the below VBA code and some test data in it. It can be downloaded from my Google Drive.

(And of course, when you see ways to make the VBA code better / faster, feel free to point it out to me as well.)

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

and

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

Solution

  • caskinfoBySKU_DYN = caskinfoBySKU(SKU, colname) instead of Call caskinfoBySKU(SKU, colname) did the trick.

    Thanks CDP1802