excel vba公式根据文本确定最高值

时间:2016-08-15 07:57:37

标签: excel excel-formula

嗨,目前我遇到了关于在一列中的4行中显示最重要文本的问题。我在这里的是客户的评论,这是优秀的,良好的,公平的和坏的...我想在一个单元格中显示优秀的单词,如果它存在于该列中,否则如果好的是存在的最高值那么它应该显示它,如果公平,那么公平或者最后如果不好那么显示不好 enter image description here

2 个答案:

答案 0 :(得分:2)

希望现在回答你的问题还为时不晚。请尝试以下公式:

=INDEX({"Bad","Fair","Good","Excellent"},MATCH(1,(MATCH({"Bad","Fair","Good","Excellent"},B2:E2,0)),0))

参见图片以供参考:

enter image description here

答案 1 :(得分:1)

这不是一个公式,但正如我所看到的,主要的麻烦不是对上面列出的四个已知值进行评级,而是排除空值和未知值。此外,当发生这种情况时,必须告知用户并做出正确的决定......

'''''''
Private Sub sb_Test_fp_Grade3()
    Debug.Print fp_Grade3(Selection, 1, True)
End Sub

Public Function fp_Grade3(pRng As Range, _
                          Optional pUnkMod& = 0, _
                          Optional pEmpDen As Boolean = False) As String
' pUnkMod - Mode of UnKnown grades handling
' 0-Ignore; 1-Info only; 2-Deny
' pEmpDen - Deny or not empty values. If Deny, then empty treated as Unknown
'           according pUnkMod setting

Const S_BAD As String = "BAD"
Const S_FAI As String = "FAIR"
Const S_GOO As String = "GOOD"
Const S_EXC As String = "EXCELLENT"
Const S_UNK As String = "UNK" ' UNKNOWN

Dim rCell As Range
Dim lVal&, lMax&, lUnk&
Dim sGrades$(0 To 4), sRet$, sVal$

    sGrades(0) = S_UNK
    sGrades(1) = S_BAD
    sGrades(2) = S_FAI
    sGrades(3) = S_GOO
    sGrades(4) = S_EXC

    lMax = 0
    lUnk = 0
    sRet = vbNullString

    For Each rCell In pRng
        sVal = rCell.Value
        If (LenB(sVal) > 0 Or pEmpDen) Then
            Select Case UCase(rCell.Value)
                Case S_BAD: lVal = 1
                Case S_FAI: lVal = 2
                Case S_GOO: lVal = 3
                Case S_EXC: lVal = 4
                Case Else:  lVal = 0
            End Select

            Select Case (lVal > 0)
                Case True                   ' Known values
                    If (lVal > lMax) Then
                        lMax = lVal
                        If (lMax = 4) Then
                            If (pUnkMod = 0) Then Exit For
                        End If
                    End If

                Case False                  ' UnKnown values
                    Select Case pUnkMod
                        Case 0      ' ignore them
                            ' do nothing
                        Case 1      ' info about them
                            lUnk = lUnk + 1
                        Case Else   ' 2 & any others - stop
                            lMax = 0
                            Exit For
                    End Select

            End Select
        End If
    Next
    If (lUnk > 0) Then sRet = " & " & lUnk & "x" & S_UNK
    sRet = sGrades(lMax) & sRet
    fp_Grade3 = sRet
End Function
'''