【问题标题】:classification of grouped items based on parent child hierarchy基于父子层次结构的分组项目分类
【发布时间】:2021-08-25 08:11:03
【问题描述】:

我有数千行带有父子层次结构的行以及我在这里分享的示例。数据具有从 0 到 10 的层次结构级别,但对我来说,级别 3 及更高级别很重要,因为我正在计算父级别 3 的权重,这取决于它的子级别和子级别。

从 L 列到 P 列,我展示了层次结构,其中 3 是父级,4 是子级,然后一些子级 4 被分类为 5、6、7……等等。父项 3 的权重是所有 4 的总和,其中 4 的总和又是 5 的总和,依此类推..

我最初尝试编写父信息。通过在 C7 中输入以下公式 =IF(B7>3;IF(B7>B6;D6;C6);"")

在第 6 行之前可以正常工作,然后由于此处的级别从 6 变为 5 而失败。请参见下图

所以我意识到 Excel 公式不足以提取所有父信息。此外,单元格 F6 再次根据材料进行分类,再次依赖于子项。

谁能告诉我如何继续使用 vba 来提取父信息。和重量分类?几行代码对我开始很有帮助。

提前非常感谢!

【问题讨论】:

  • 在这种情况下,父级别 3 的权重是单元格 F6,因为层次结构(B 列)3 是父级别,父级别 1 是它的名称。 F:F 列基于以下任一标准求和:(1) 父级或 (2) 可能基于层次结构。层次结构级别 3(第 6 行)的权重是所有层次结构 4(第 7 行和第 8 行)的权重之和,层次结构 4(第 8 行)的权重是 5 的权重之和(第 9,11 行)。层次结构 5 的权重是 6 的总和(第 12,13 行)。 L:P 仅用于说明。如果没有其他子类别(如第 12 行和第 14 行),则权重仅适用于最低层次结构。是的,权重将被分析
  • 我正在查看第二张图片并试图理解为什么第二个 5 应该是 Sub Part 1 而不是 Part2 在我看来,哪个孩子看起来......不是第二个 4 的孩子?
  • 你说得对,第二个 5 是 Part 2 的孩子(第二个 4 的孩子)。在第二张图片中,我使用 Excel 公式使用 =IF(B7>3;IF(B7>B6;D6;C6);"") 提取 C:C 中的父名称。这个公式没有完全解决当层次结构级别降低时如何识别父级,在这种情况下是从 6 到 5。我在我的帖子中说明了这一点“我试图通过在 C7 中放入以下内容来最初编写每个孩子的父信息。公式 =IF(B7>3;IF(B7>B6;D6;C6);"") 可以正常工作到第 6 行,然后由于此处的级别从 6 变为 5 而失败。请参见下图“
  • 然后,试试我粘贴的代码...
  • 请测试更新的代码。第一个版本在 F:F 列中返回处理后的数组。只是为了测试期间...在复制上一个版本之前刷新页面(这个)...

标签: excel vba parent-child


【解决方案1】:

请测试下一个代码。您没有回答我的澄清问题,以下代码假定您没有向我们展示第二个5 的正确重量:

Sub CalculateWeight()
   Dim sh As Worksheet, lastR As Long, arr, arrC, ref As Long, i As Long, j As Long, k As Long

   Set sh = ActiveSheet
   lastR = sh.Range("B" & sh.rows.count).End(xlUp).row
   arr = sh.Range("B3:D" & lastR).Value 'put the range to be processed in an array, for faster iterations
   ReDim arrC(1 To UBound(arr), 1 To 1) 'redim the final array for the same number of rows like arr

   For i = 1 To UBound(arr)             'iterate between the array rows
        If arr(i, 1) > 3 Then           'if the hierarchy value is > 3:
            Do While (arr(i + j, 1) > 3)'loop until the next 0
                ref = i - 1             'memorize the row keeping the third hierarchy
                If arr(i + j, 1) = arr(i + j - 1, 1) + 1 Then      'if the value in column 1 is less with a unit thay precedent
                     arrC(i + j, 1) = arr(i + j - 1, 3): j = j + 1 'take the value of the precedent row, third column
                Else
                    For k = i + j To ref Step -1                   'iterate backwards
                        If arr(i + j, 1) = arr(k - 1, 1) + 1 Then  'when find the hierarchy less with a unit
                            arrC(i + j, 1) = arr(k - 1, 3): j = j + 1: Exit For 'take the value of third column and exit iteration
                        End If
                    Next k
                    
                End If
                If i + j > UBound(arr) Then Exit For  'exit iteration if it exceeds the array number of elements
            Loop
        Else
            arrC(i, 1) = ""                            'for lines before each 3
        End If
        If j > 0 Then i = i + j - 1: j = 0  'reinitialize variables
   Next i
   sh.Range("C3").Resize(UBound(arrC), 1).Value = arrC 'drop the array content at once
End Sub

【讨论】:

  • 您的代码完美地提取了子名称旁边的所有父名称。非常感谢你:)
猜你喜欢
  • 2018-03-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-31
  • 1970-01-01
  • 1970-01-01
  • 2011-04-23
相关资源
最近更新 更多