我有一个具有以下结构的Excel数据库:
我可以使用嵌套的IF公式,但组和子组中的条目数可能会有很大差异。到目前为止,我还没有设法构建一个有效的嵌套循环。
答案 0 :(得分:1)
这是一个基于字典的方法,你应该能够调整:
Sub LabelSubgroups(R As Range)
'R is assumed to be a 3-column range
'Consisting of the data without the header as well as the
'final column to contain the results
'Note that the final column will be overwritten
Dim i As Long, j As Long, n As Long
Dim key1 As Variant, key2 As Variant
Dim D As Object, Grp As Object
Dim A As Variant
n = R.Rows.Count
A = R.Value
Set D = CreateObject("Scripting.Dictionary")
For i = 1 To n
key1 = A(i, 2)
key2 = A(i, 1)
If D.Exists(key1) Then
If Not D(key1).Exists(key2) Then
D(key1).Add key2, D(key1).Count + 1
End If
Else
Set Grp = CreateObject("Scripting.Dictionary")
Grp.Add key2, 1
D.Add key1, Grp
End If
Next i
For i = 1 To n
key1 = A(i, 2)
If D(key1).Count > 1 Then
key2 = A(i, 1)
A(i, 3) = key1 & "-" & D(key1)(key2)
Else
A(i, 3) = key1
End If
Next i
R.Value = A
End Sub
如果您对样本数据运行LabelSubgroups Range("A2:C12")
,那么它会产生所需的输出。
答案 1 :(得分:1)
这是一个带有嵌套IF 数组公式的NOT-VBA解决方案:
{'id': fields.Integer, 'document_ids':fields.List(fields.Integer, attribute='documents.id')}
{'id': fields.Integer, 'document_ids':fields.List(fields.Nested({'id':fields.Integer}), attribute='documents')}
按CTRL-ALT-Return将其输入为 array-formula
复制C2中的公式并将其从C3向下粘贴