如果在 D 列中找到字符串“set_path”或“stop”,我不确定该值应该是什么,但如果它们始终相同(即“a”,如果 D 列 =“set_path”和“ stop" 如果 D 列 = "stop"),您可以使用以下公式达到您想要的结果:
=IF(AND(D2<>"set_path",D2<>"stop"),COUNTIF($D$1:D2,"is_null")+COUNTIF($D$1:D2,"equals"),IF(D2="set_path","a",IF(D2="stop","stop","")))
更新:
使用 VBA,您可以保留单元格的内容,而无需使用以下代码使用公式覆盖它们:
Sub foo()
Dim ws As Worksheet: Set ws = Sheets("Sheet1")
'declare and set the worksheet you are working with, amend as required.
Dim LastRow As Long, i As Long
LastRow = ws.Cells(ws.Rows.Count, "D").End(xlUp).Row
'get the last row with data on Column D
For i = 2 To LastRow
If ws.Cells(i, "B").Value = "" Then 'if cell is empty add formula
ws.Cells(i, "B").FormulaR1C1 = "=COUNTIF(R1C4:RC[2],""is_null"")+COUNTIF(R1C4:RC[2],""equals"")"
End If
Next i
End Sub
第二次更新:
如果在 D 列中找到“set_path”,我现在已经调整了公式以将字母加一(请记住,这将从 AZ 开始,然后它将根据 ASCII 表开始遍历符号,所以如果你想要替代行为,你可能需要修改它):
=IF(AND(D2<>"set_path",D2<>"stop"),COUNTIF($D$1:D2,"is_null")+COUNTIF($D$1:D2,"equals"),IF(D2="set_path",IFERROR(CHAR(COUNTIF($D$2:D2,"set_path")+64),""),IF(D2="stop","stop","")))