if-countif超过7

时间:2016-11-19 20:20:02

标签: excel vba

我在excel文件上有一点问题。我使用下面的公式来达到:

如果所选单元格包含另一个值(字符串),则该函数返回一个值。这项工作很完美。但我有20个if / countif要添加它不起作用因为我不能超过7 ... 有没有想过轻松解决这个问题? 我还给你excel代码下的vba代码......

非常感谢您的帮助

=SI(NB.SI([@[ENTRY_LABEL]];"*MAZ*");"MAZ";SI(NB.SI([@[ENTRY_LABEL]];"*MGN*");"MGN";SI(NB.SI([@[ENTRY_LABEL]];"*Magnitude*");"MGN";SI(NB.SI([@[ENTRY_LABEL]];"*AJU*");"AJU";SI(NB.SI([@[ENTRY_LABEL]];"*Reclas*");"Reclass";"")))))

VBA代码:

Range(ActiveCell, ActiveCell.End(xlDown)).SpecialCells(xlCellTypeVisible).FormulaR1C1 = "=IF(COUNTIF(Tableau4[[#This Row],[ENTRY_LABEL]],""*MAZ*""),""MAZ"",IF(COUNTIF(Tableau4[[#This Row],[ENTRY_LABEL]],""*MGN*""),""MGN"",IF(COUNTIF(Tableau4[[#This Row],[ENTRY_LABEL]],""*Magnitude*""),""MGN"",IF(COUNTIF(Tableau4[[#This Row],[ENTRY_LABEL]],""*AJU*""),""AJU"",IF(COUNTIF(Tableau4[[#This Row],[ENTRY_LABEL]],""*Reclas*""),""Reclass"","""")))))"

2 个答案:

答案 0 :(得分:1)

我终于使用了它并且它有效。

Public Function cat1_0lia(my_cell As Range) As String

    Dim result  As String

    Select Case True

    Case my_cell.Value Like "*MAZ*":
    result = "MAZ"
    Case my_cell.Value Like "*Maz*":
    result = "MAZ"
    Case my_cell.Value Like "*maz*":
    result = "MAZ"
    Case my_cell.Value Like "*Mis à 0*":
    result = "MAZ"

    Case my_cell.Value Like "*Mgn*":
    result = "MGN"
    Case my_cell.Value Like "*MGN*":
    result = "MGN"
    Case my_cell.Value Like "*Magnitude*":
    result = "MGN"

    Case my_cell.Value Like "*AJU*":
    result = "AJU"
    Case my_cell.Value Like "*Aju*":
    result = "AJU"
    Case my_cell.Value Like "*aju*":
    result = "AJU"


    Case my_cell.Value Like "*RECLASS*":
    result = "RECLASS"
    Case my_cell.Value Like "*Reclass*":
    result = "RECLASS"
    Case my_cell.Value Like "*reclass*":
    result = "RECLASS"

    Case Else:
    result = ""


    End Select

    cat1_0lia = result

End Function

答案 1 :(得分:0)

在这种情况下,您可以使用VBA公式。只需在Visual Basic编辑器中,在模块级别中编写类似的内容:

Public Function many_ifs(my_cell As Range) As String

Select Case my_cell.Value

    Case 1
        many_ifs = "one"

    Case 2
        many_ifs = "two"

    Case 3
        many_ifs = "three"

    Case 4
        many_ifs = "four"

    Case Else:
        many_ifs = "Unknown"

End Select

End Function

然后从Excel中将其称为普通公式,如下所示:

enter image description here

以下是如何开始使用VBA: https://msdn.microsoft.com/en-us/library/office/ee814737(v=office.14).aspx