Small Large Formula Convert to Acess VBA Calculated Field Query

感情迁移 提交于 2020-01-06 14:40:58

问题


I am trying to convert the following excel formula to a Calculated Field in Access. Using Expressions or combination of a user defined expression with VBA code. any help appreciated! And appreciate any thoughts about data normalization, however this is not the issue. Thx

IF(SMALL(T319:V319,2)>6,SMALL(T319:V319,1),IF(SMALL(T319:V319,2)=0,LARGE(T319:V319,1),SMALL(T319:V319,2)

Cells T319 - V319 are fields in each record generated by a data query generated. call them A,B,C for this purpose.


回答1:


Suppose I presort the data so that from largest to smallest, the three values are represented by the letters I, J, and K. This means J is SMALL(T319:V319,2), K is SMALL(T319:V319,1) and I is LARGE(T319:V319,1).

UPDATE: fixed the pseudocode and final section of the function. I misread the nested IFs of the Excel formula :P

The pseudo-code would be:

If J > 6 Then
    return K
ElseIf J = 0 Then
    return I
Else
    return J

Are you sure this is what you want?

If so, you could make a VBA function as follows:

Public Function GetVal(A As Variant, B As Variant, C As Variant)
    Dim I As Variant
    Dim J As Variant
    Dim K As Variant

    'Sort the three values so A, B, C becomes sorted largest to smallest as I, J, K
    If A > B Then
        If A > C Then
            I = A
            If B > C Then
                J = B
                K = C
            Else 'C > B
                J = C
                K = B
            End If
        Else 'C > A
            I = C
            J = A
            K = B
        End If
    Else 'B > A
        If B > C Then
            I = B
            If A > C Then
                J = A
                K = C
            Else 'C > A
                J = C
                K = A
            End If
        Else 'C > B
            I = C
            J = B
            K = A
        End If
    End If

    'Use I, J, K to find the result
    If J > 6 Then
        GetVal = K
    ElseIf J = 0 Then
        GetVal = I
    Else
        GetVal = J
    End If
End Function


来源:https://stackoverflow.com/questions/23371605/small-large-formula-convert-to-acess-vba-calculated-field-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!