繁体   English   中英

计算并显示连续唯一值的出现次数

[英]Count and Display Occurrences of Unique values in a row

我有最多可容纳 4 件物品的托盘。 有时同一物品是一个托盘。 要打印此托盘的运输 label,我需要列出托盘上的物品和该物品的数量。 我发现了一些公式和脚本来计算列的唯一实例,但不是保留在同一行中的数据。 以下是我收到的数据:

在此处输入图像描述

我需要把它变成这种格式: 在此处输入图像描述

我已经手动完成了上述操作,以显示我需要一个公式或脚本来做什么。 我一整天都在试图解决这个问题,因此感谢您的帮助。 谢谢!

计数和重新排列

  • 调整常量部分中的值。

编码

Option Explicit

Sub countRearrange()
    
    ' Define constants.
    Const srcName As String = "Sheet1"
    Const srcAddress As String = "G2:J4"
    Const dstName As String = "Sheet2"
    Const dstAddress As String = "S2:Z4"
    
    ' Define workbook.
    Dim wb As Workbook: Set wb = ThisWorkbook ' Workbook containing this code.
    
    ' Write values from Source Range to Data Array.
    Dim Data As Variant: Data = wb.Worksheets(srcName).Range(srcAddress).Value
    Dim cCount As Long: cCount = UBound(Data, 2)
    
    ' Define Destination Range.
    Dim rg As Range: Set rg = wb.Worksheets(dstName).Range(dstAddress)
    
    ' Define Result Array.
    Dim Result As Variant
    ReDim Result(1 To rg.Rows.Count, 1 To rg.Columns.Count)
    
    ' Declare additional variables.
    Dim Key As Variant ' Current Data Array Value
    Dim i As Long ' Data Array Row Counter, Result Array Row Counter
    Dim j As Long ' Data Array Column Counter
    Dim n As Long ' Dictionary Element Counter, Result Array Column Counter
    Dim x As Long ' Result Array Column Counter
    
    ' Write values from Data Array to Result Array.
    With CreateObject("Scripting.Dictionary")
        .CompareMode = vbTextCompare
        For i = 1 To UBound(Data, 1)
            For j = 1 To cCount
                Key = Data(i, j)
                If Not IsError(Key) Then
                    If Len(Key) > 0 Then
                        If Not .Exists(Key) Then
                            n = n + 1
                            .Item(Key) = n
                            Result(i, 2 * n - 1) = Key
                            Result(i, 2 * n) = 1
                        Else
                            x = .Item(Key)
                            Result(i, 2 * x - 1) = Key
                            Result(i, 2 * x) = Result(i, 2 * x) + 1
                        End If
                    End If
                End If
            Next j
        Next i
    End With
        
    ' Write values from Result Array to Destination Range.
    rg.Value = Result
    
End Sub

您也可以使用 Excel 2010+ 中提供的Power Query来执行此操作

  • Select 表格中的一个单元格
    • Data => Get & Transform => From Table/Range
  • 当 Power Query UI 打开时
    • Home => Advanced Editor并将 M 代码粘贴到打开的 window 中
    • 更改第 2 行中的表名称以反映给定数据的真实表名称
  • 阅读代码中的注释,并检查Applied Steps window 以了解其工作原理

M代码

let
    Source = Excel.CurrentWorkbook(){[Name="Table11"]}[Content],

#"Changed Type" = Table.TransformColumnTypes(Source,{{"Item #1", type text}, {"Item #2", type text}, {"Item #3", type text}, {"Item #4", type text}}),
    #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1, Int64.Type),
    #"Grouped Rows" = Table.Group(#"Added Index", {"Index"}, {
        {"Grouped", each _, type table [#"Item #1"=nullable text, #"Item #2"=nullable text, #"Item #3"=nullable text, #"Item #4"=nullable text, Index=number]}}),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "uniqueItemList", each 
        let 
            //Get list of relevant column Names for grouping of each subTable
            colNames = Table.ColumnNames(Source),
            tbl1 = Table.SelectColumns([Grouped],colNames),

            //Transpose the table
            transp = Table.Transpose(tbl1),

            //Get a count of each unique Item
            grp = Table.Group(transp,"Column1",{"count", each List.Count([Column1])}),

            //create Lists of the items and their count and "Zip" them together
            col1 = Table.Column(grp,"Column1"),
            count = Table.Column(grp,"count"),
            zip = List.Zip({col1,count}),

            //create the Label string
            label = List.Accumulate(zip,"", (state, current)=> 
                if state = ""
                    then current{0} & ";" & Text.From(current{1}) 
                    else 
                        if List.NonNullCount(current) = 2 
                            then state & ";" & current{0} & ";" & Text.From(current{1})
                            else state)
        in 
            label),

    //split the label string into separate columns
    #"Split Column by Delimiter" = Table.SplitColumn(#"Added Custom", "uniqueItemList", Splitter.SplitTextByDelimiter
        (";", QuoteStyle.Csv), {"Slot #1", "Slot #1 Qty", "Slot #2", "Slot #2 Qty", "Slot #3", "Slot #3 Qty","Slot #4", "Slot #4 Qty"}),

    //Delete unneeded columns    
    #"Removed Columns" = Table.RemoveColumns(#"Split Column by Delimiter",{"Index", "Grouped"}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Removed Columns",{{"Slot #1", type text}, {"Slot #1 Qty", Int64.Type}, {"Slot #2", type text}, {"Slot #2 Qty", Int64.Type}, {"Slot #3", type text}, {"Slot #3 Qty", Int64.Type}, {"Slot #4", type text}, {"Slot #4 Qty", Int64.Type}})
in
    #"Changed Type1"

在此处输入图像描述

我想这就是你要找的。

样品解决方案

AA2 是=TRANSPOSE(UNIQUE(TRANSPOSE(FILTER(G2:J2,G2:J2<>"")))) TRANSPOSE是必要的,因为UNIQUE垂直工作。 AB2:AD2 中的值是从 AA2 溢出的。

AE2 是=IF(AA2#="","",COUNTIF(G2:J2,AA2#)) AF2:AH2 从 AE2 溢出。

S2 为=IF(INDEX(AA2:AH2,{1,5,2,6,3,5,4,8})=0,"",INDEX(AA2:AH2,{1,5,2,6,3,7,4,8})) T2:Z2 从 S2 溢出。

第 3 行和第 4 行类似。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM