我在 A 列中有一些数据,如下所示
ColA
Z
A
Z
A
Z
A
A
B
B
B
Z
B
B
Z
C
C
C
D
D
我想在 B 列打印 也就是说,为每个新组交替分配值 1 和 2。我希望有意义。
这是我当前的代码
Sub t1()
Dim dict As Object
Set dict = CreateObject("Scripting.Dictionary")
For i = 1 To 24
If Cells(i, "A") = "Z" Then
Cells(i, "B") = "0"
ElseIf Cells(i, "A") <> "Z" And Cells(i, "A") <> "" Then
counter = counter + 1
dict.Add Key:=Cells(i, "A"), Item:=1
If Not dict.Exists(Cells(i, "A")) Then
If counter Mod 2 = 1 Then
Cells(i, "B") = "1"
Else
Cells(i, "B") = "2"
End If
End If
End If
Next
End Sub
我在 B 列中的当前输出和我在列单元格中的预期输出ColA ColB ColC
Z 0 0
A 1 1
Z 0 0
A 2 1
Z 0 0
A 1 1
A 2 1
B 1 2
B 2 2
B 1 2
Z 0 0
B 2 2
B 1 2
Z 0 0
C 2 1
C 1 1
C 2 1
D 1 2
D 2 2
也许有人可以帮助我。谢谢
最佳答案
Hello, no. Imagine Z and empty values don't exist. Then we have this input A,A,A,B,C,C,C,C,D,D,A,E,E,E,B,A. I only want the alternation between 1 and 2 for each new group. Doesn't matter if A has appeared before, should be taken as new group. With this input the, output should be 1,1,1,2,1,1,1,1,2,2,1,2,2,2,1,2 – Ger Cas 9 hours ago
试试这个疯狂的公式(我相信这可以变得更简单)。将其放入单元格
B2
如下图所示=IF(ISBLANK(A2),"",IF(A2="Z",0,IF(A2=A1,B1,IF(OR(A1="Z",ISBLANK(A1)),IF(ISERROR(INDEX($A$1:$A1,AGGREGATE(14,6,ROW($1:1)/($A$1:$A1=A2),1))),IFERROR((A1+1),1),IF(OR(INDEX($A$1:$A1,AGGREGATE(14,6,ROW($1:1)/($A$1:$A1=A2),1)+1)="Z",INDEX($A$1:$A1,AGGREGATE(14,6,ROW($1:1)/($A$1:$A1=A2),1)+1)=""),INDEX(B:B,MATCH(A2,$A$1:$A1,0),1),1)),IF(B1=1,2,IF(B1=2,1,""))))))
假设您的数据看起来像这样解释
按照以下顺序进行一系列检查
0
. INDEX
与 AGGREGATE()
进行反向匹配以查找该值的出现,如果找到匹配项,则从列 B
中提取相应的值关于excel - 如何确定连续值组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63083653/