提问人:jsalale 提问时间:11/16/2023 最后编辑:horseyridejsalale 更新时间:11/16/2023 访问量:88
有没有一种简单的方法来总结行和列重复的 Excel 工作表?
Is there a straightforward way to summarize an excel sheet where the rows and columns repeat?
问:
我有一个文件,其中第一列中列出了部分,这些部分重复,之后的每一列都有这些部分的位置,并且这些部分确实根据日期重复。
我需要总结成一个行或列重复的单张表,它们只是“求和”行和列是相同的。
总结一下:
进入这个:
首先,我尝试遍历行和列,找到重复项并将它们相加。灾难,在简单的 10 x 10 表中一切正常,但完整文件有 600+ 行和 700+ 列,它循环了几个小时,我不得不重新启动 PC。目前,我正在尝试使用VBA复制选择,在临时表中粘贴转置,排序,然后:
For i = filterLastRow To 1 Step -1
If shtFCol.Range("B" & i) = shtFCol.Range("B" & i - 1) Then
For j = 6 To LastCol
shtFCol.Range("B" & i - 1) = shtFCol.Range("B" & i - 1) + shtFCol.Range("B" & i)
Next j
shtFCol.Rows(i).EntireRow.Delete
i = i - 1
End If
Next i
粘贴回原始内容,然后对所有行重复该过程。有没有更简单/更快的方法?
答:
2赞
Tim Williams
11/16/2023
#1
这里有一种方法。结果放在单独的工作表中。
Sub test()
Dim wsResult As Worksheet
Dim rng As Range, arr, r As Long, c As Long, mR, mC
Set wsResult = ThisWorkbook.Worksheets("Result") 'sheet for result
wsResult.Cells.ClearContents
Set rng = ThisWorkbook.Worksheets("Data").Range("B4:I12") 'source data
arr = rng.Value
For r = 2 To UBound(arr, 1) 'loop input rows from row 2
mR = Application.Match(arr(r, 1), wsResult.Columns(1), 0) 'already have row header?
If IsError(mR) Then 'if no mtch found...
mR = wsResult.Cells(Rows.Count, 1).End(xlUp).row + 1 'next empty cell in column A
wsResult.Cells(mR, 1).Value = arr(r, 1) 'add the row header
End If
For c = 2 To UBound(arr, 2) 'loop input columns
mC = Application.Match(arr(1, c), wsResult.Rows(1), 0) 'follow same process as for row headers
If IsError(mC) Then
mC = wsResult.Cells(1, Columns.Count).End(xlToLeft).Column + 1
wsResult.Cells(1, mC).Value = arr(1, c)
End If
With wsResult.Cells(mR, mC)
.Value = .Value + 1 'increment the count for this unique pair of row/column label
End With
Next c
Next r
End Sub
2赞
horseyride
11/16/2023
#2
可以使用数据将范围引入 powerquery ...from table/range [ ] 标头
回家......高级编辑器...并从下面粘贴。使用自动生成的第一行代替我的第一行来保留您的范围名称
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content], //use your auto generated first row in place of this one
#"Promoted Headers" = Table.PromoteHeaders(Source, [PromoteAllScalars=true]),
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Promoted Headers", {"Column1"}, "Attribute", "Value"),
Split_Dupes = Table.TransformColumns(#"Unpivoted Other Columns",{{"Attribute", each Text.BeforeDelimiter(_,"_"), type text}}),
#"Pivoted Column" = Table.Pivot(Split_Dupes, List.Distinct(Split_Dupes[Attribute]), "Attribute", "Value", List.Sum)
in #"Pivoted Column"
文件。。。关闭并加载...表格返回 Excel
右键单击并在需要时刷新表,或 VBA 刷新 PowerQuery
评论