我有以下类型的表
Current table
P.no. P.Line ian feb mar apr mai iun iul aug sept oct nov dec
100001 Line1 100
100001 Line1 100
100001 Line1 300
100001 Line2 200
100002 Line2 100
100003 Line3 100
100003 Line3 100
100003 Line3 100
100003 Line3 300
100003 Line3 200
100003 Line3 100
我需要让它看起来像这样:
Needed result
P. no. P.line ian feb mar apr mai iun iul aug sept oct nov dec
100001 Line1 100 100 300
100001 Line2 200
100002 Line2 100
100003 Line3 100 100 100 300 200 100
先感谢您。
答案1
您可以创建单独的汇总表并使用 SUMIFS。我创建了一个如下所示的表格大纲:
P.no. P.Line ian feb mar apr mai iun iul aug sept oct nov dec
100001 Line 1
100001 Line 2
100002 Line 2
100003 Line 3
我通过复制 A 和 B 并使用 Excel 的删除重复项来实现此目的。此公式可用于对每列求和(假设您的数据从 A 列开始,ian 在 C 列中):
=SUMIFS(C$2:C$12, $A$2:$A$12, $A15, $B$2:$B$12, $B15)
这将对 C 列求和,其中 A 列与 A15 相同,B 列与 B15 相同。您可以向右向下拖动此公式,它应该是动态的。
在您的示例中,如果没有数据,则返回空白,这也可以做到,但您需要将上述内容包装成如下内容:
=IF(SUMIFS(C$2:C$12, $A$2:$A$12, $A15, $B$2:$B$12, $B15) = 0, "", SUMIFS(C$2:C$12, $A$2:$A$12, $A15, $B$2:$B$12, $B15))
这只是意味着如果总和为 0 则返回空白,否则给我总和。
答案2
以 s 作为源范围,t 作为目标范围,我会执行以下操作:
' loop through the rows
for i=1 to s.rows.count
' check if the sum code is changed
if s(i,1) & s(i,2) <> prev_id then trow = trow +1
' fill the target row with the source values
for j=1 to s.columns.count
if not isempty(s(i,j)) then t(trow, j) = s(i,j)
next
' save the sum code (for the next loop)
prev_id = s(i,1) & s(i,2)
next
修订:好的,根据您的链接我做了以下操作:
Sub CombineRows()
Dim rw As Long
Dim col As Long
Dim rng As Range
' make sure the cursor is somewhere in the left columns
Set rng = Selection.CurrentRegion
For rw = rng.Rows.Count To 2 Step -1
If rng(rw, 2) = rng(rw - 1, 2) And rng(rw, 3) = rng(rw - 1, 3) Then
For col = 4 To rng.Columns.Count
If Not IsEmpty(rng(rw, col).Value2) Then
rng(rw - 1, col).Value2 = rng(rw, col).Value2
End If
Next
rng.Rows(rw).EntireRow.Delete
End If
Next
End Sub
只需站在左边三列的某个地方并运行宏。