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

3 个回复

## 计数和重新排列

• 调整常量部分中的值。

``````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)

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
``````

• 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}}),
#"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]}}),
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 溢出。

1 如何计算MATLAB中唯一行的出现次数？

2017-10-17 02:44:15 2 352   matrix
4 如何计算R中一行中特定值的出现次数

2011-05-15 19:34:51 4 5499   r/ rowsum
5 如何根据两列值计算前几行中的出现次数

6 计算mySQL中每一行的出现次数

8 计算多列中每个唯一行的字符串出现次数

2019-11-11 16:36:59 1 165   pandas
9 新列显示唯一行的出现次数

2015-06-06 19:49:03 1 127   pandas