excel - 删除列中的重复项并在另一列中输入 Sum
问题描述
我想根据 I 列中的文本删除重复项并对 C 列中的值求和,其他列中的数据无关紧要。
我不想要数据透视表,而且我知道它们是这类事物的首选。
我想实现的一个例子:
我找到了 VBA 代码并试图修改它。它不会删除所有行。
Sub Sum_and_Dedupe()
With Worksheets("data")
'deal with the block of data radiating out from A1
With .Cells(1, 1).CurrentRegion
'step off the header and make one column wider
With .Resize(.Rows.Count - 1, .Columns.Count + 1).Offset(1, 0)
.Columns(.Columns.Count).Formula = "=sumifs(c:c, i:i, i2)"
.Columns(3) = .Columns(.Columns.Count).Value
.Columns(.Columns.Count).Delete
End With
'remove duplicates
.RemoveDuplicates Columns:=Array(9), Header:=xlYes
End With
.UsedRange
End With
End Sub
解决方案
您需要将当前工作表名称更改为data,或者更改此代码的前两行以满足您的需要。sh
= 您向我们展示的数据表。osh
= 此代码将生成的输出表。另请注意,如果 C 列或 I 列移动,您可以通过更改colBooked
和轻松更新位置colEstimate
。如果您有超过一千个唯一估计条目,则使数组编号大于 999。
Sub summariseEstimates()
Dim sh As String: sh = "data"
Dim osh As String: osh = "summary"
Dim colBooked As Integer: colBooked = 3
Dim colEstimate As Integer: colEstimate = 9
Dim myArray(999) As String
Dim shCheck As Worksheet
Dim output As Worksheet
Dim lastRow As Long
Dim a As Integer: a = 0
Dim b As Integer
Dim r As Long 'row anchor
Dim i As Integer 'sheets
'Build summary array:
With Worksheets(sh)
lastRow = .Cells.Find("*", searchorder:=xlByRows, searchdirection:=xlPrevious).Row
For r = 2 To lastRow
If r = 2 Then 'first entry
myArray(a) = .Cells(r, colEstimate) & "," & .Cells(r, colBooked)
Else
For b = 0 To a
If VBA.LCase(VBA.Replace(.Cells(r, colEstimate), " ", "")) = VBA.LCase(VBA.Replace(VBA.Split(myArray(b), ",")(0), " ", "")) Then 'match
myArray(b) = VBA.Split(myArray(b), ",")(0) & "," & VBA.Split(myArray(b), ",")(1) + .Cells(r, colBooked)
Exit For
End If
Next b
If b = a + 1 Then 'completed loop = no match, create new array item:
a = a + 1
myArray(a) = .Cells(r, colEstimate) & "," & .Cells(r, colBooked)
End If
End If
Next r
End With
'Create summary sheet:
On Error Resume Next
Set shCheck = Worksheets(osh)
If Err.Number <> 0 Then
On Error GoTo 0
Set output = Worksheets.Add(After:=Worksheets(sh))
output.Name = osh
Err.Clear
Else
On Error GoTo 0
If MsgBox("*" & osh & "* sheet already exists. Proceed to delete and recreate?", vbOKCancel, "Summary") = vbCancel Then
Exit Sub
Else
Application.DisplayAlerts = False
Worksheets(osh).Delete
Set output = Worksheets.Add(After:=Worksheets(sh))
output.Name = osh
End If
End If
'Output to summary sheet:
With Worksheets(osh)
.Cells(1, 1).Value = "ESTIMATE"
.Cells(1, 2).Value = "BOOKED THIS WEEK"
For b = 0 To a
.Cells(b + 2, 1).Value = VBA.Split(myArray(b), ",")(0)
.Cells(b + 2, 2).Value = VBA.Split(myArray(b), ",")(1)
Next b
.Columns("A:B").AutoFit
End With
End Sub
推荐阅读
- android - Android drag to pan, navigate through app
- python - Go to definition is not working with docker and vagrant
- xmpp - 实现无状态 ejabberd 架构所需的建议或帮助
- python - How do you determine what attributes and methods are available in PloneFormGen
- c# - .Net Core Docker Image 在 docker 中运行时无法访问数据库
- java - 将 T 参数转换为适当的具体类型
- c# - OPOS Bridge for UWP PointOfService 是否支持 OPOS 条码扫描仪
- python - 在 Python 的 zipfile 中提取 txz 文件
- javascript - 为什么我在 JSS 样式表中声明的 woff 没有加载 @font-face?
- python - 在 python 中有没有 exec 的替代方法?