2016-11-19 53 views
1

我對excel文件有點問題。我使用下面的公式來達到以下目的:if-countif超過7

如果所選單元格包含另一個值(字符串),則函數會返回一個值。這工作完美。但我有20如果/ countif添加,它不工作,因爲我不能得到7 ... 任何想法很容易解決這個問題? 我給你也VBA代碼Excel的代碼下...

非常感謝幫助

=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"","""")))))" 

回答

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

+1

謝謝你的回答,它的工作原理 – jmten

+0

謝謝你!很高興看到你能夠用你自己的方式來修正這個公式! :) – Vityata

+0

@Vityata爲我的編輯辯解,但只是爲了使它更乾淨,更短 –

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