分组数据表

问题描述:

我曾在一个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 
+0

正如你所提到我已经改变了原来的响应(把它在一个评论是小于全部清除) –

+0

非常感谢你“鲍勃·菲利普斯”为你的伟大的代码,我只需要小的修改,对于 最终结果每个客户..在你的代码中你是不算数的。为1,在我的情况下,我需要最大值,所以在我的兴趣结果我对结果1和0的兴趣,你可以将最大值选择任何值,如果0或1它只产生1,再次感谢 –

+0

伟大的工作,谢谢 –

VBA不需要这个。选择您的所有数据和资料>大纲 - 小计:

在每个变化:customer ID
使用功能:最大
添加小计:检查每个attributes(只)
检查的替换当前分类汇总及以下摘要数据
确定

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

+1

谢谢Pnuts,很好的解决方案 –