分组数据表

我在Excel文件中有以下表格:

customer ID sell attribute1 attribute2 attribute3 …… attribute N Customer1 sell1 1 0 1 1 Customer1 sell2 0 0 0 0 Customer1 sell3 1 0 1 1 Customer2 sell4 0 0 0 1 Customer2 sell5 1 0 1 0 Customer3 sell6 1 0 0 0 …… …… 

我需要一个Excel VBA函数来删除多余的客户行,并为每个客户只产生一行代表该客户的所有列值…(每列的最大值只产生该列中的最大值),列出售其不考虑在最后的结果。

结果如下:

 customer ID attribute1 attribute2 attribute3 …… attribute N Customer1 1 0 1 1 Customer2 1 0 1 1 Customer3 1 0 0 0 

等等….

试试这个简单的小程序

 Sub MergeData() Dim lastrow As Long Dim lastcol As Long Dim i As Long, ii As Long Application.ScreenUpdating = False With ActiveSheet lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row lastcol = .Cells(1, .Columns.Count).End(xlToLeft).Column For i = lastrow To 2 Step -1 If .Cells(i, "A").Value = .Cells(i - 1, "A").Value Then For ii = 3 To lastcol .Cells(i - 1, ii).Value = -(.Cells(i - 1, ii).Value > 0 OR .Cells(i, ii).Value > 0) Next ii .Rows(i).Delete End If Next i .Columns(2).Delete End With Application.ScreenUpdating = True End Sub 

VBA不是必需的。 select所有的数据和数据>大纲 – 小计:

每次更改: customer ID
使用function:最大
添加小计到:检查每个attributes (仅)
检查replace当前小计和摘要下面的数据

复制并粘贴特殊…,值在顶部。 筛选以selectColumnA,文本筛选器,包含…,最大值,确定,select可见数据(不包括最后一行,如果不需要),复制并粘贴到需要的位置。 删除ColumnB。