免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1190 | 回复: 0
打印 上一主题 下一主题

Excel不定行转置 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-01-12 15:27 |只看该作者 |倒序浏览
假期中,接到业务一个Excel的求助电话,比较复杂,结合百度,经过一晚上的努力,幸不辱命。
还是看数据说话,有一个Excel数据表A,原型如下:
编码   名称      供应商
111   阿莫西林   公司AAA
111   阿莫西林   公司BBB
111   阿莫西林   公司CCC
222   达克宁     公司CCC
222   达克宁     公司BBB
333   康泰克     公司BBB
444   阿莫西林1   公司AAA
444   阿莫西林1   公司BBB
444   阿莫西林1   公司CCC  
 
希望得到数据表B,处理结果如下:
编码   名称      供应商1      供应商2      供应商3     供应商4  ...
111   阿莫西林   公司AAA      公司BBB      公司CCC
222   达克宁     公司CCC      公司BBB
333   康泰克     公司BBB
444   阿莫西林1   公司AAA     公司BBB      公司CCC
 
很显然,这是一个转置问题,但是有两个问题:
1.原始表A中数据巨大,近十万行记录,而且由原始表-> 结果表的过程希望可以重复;
2.原始表中每一个编码的记录行数不确定。
总结起来就是 “不定行转置,或者分组转置”
 
解决方案:
首先想到的把原始表导入数据库,用sql实现;好处是快速,缺点是门槛太高,需要信息部门介入,所以用VBA是一个比较合理的方案,具体算法如下:
 
  1. Sub MySort()

  2.     '取消筛选
  3.     Workbooks(1).Activate
  4.     If ActiveSheet.AutoFilterMode Then '如果 表2 当前为自动过滤状态
  5.         Selection.AutoFilter '取消过滤
  6.     End If
  7.     
  8.         
  9.     '先排序
  10.     Columns("A:O").Select
  11.     Selection.Sort Key1:=Range("A1"), Order1:=xlAscending, Key2:=Range("C1") _
  12.         , Order2:=xlAscending, Header:=xlGuess, OrderCustom:=1, MatchCase:= _
  13.         False, Orientation:=xlTopToBottom, SortMethod:=xlPinYin, DataOption1:= _
  14.         xlSortNormal, DataOption2:=xlSortNormal
  15.     
  16.         
  17.     Dim RowCount, ColCount, FlagCol
  18.     '取最大行数,excel2007 max rows =1048576,excel2003 max rows =65536
  19.     'RowCount = Selection.Rows.Count
  20.     RowCount = Range("A1048576").End(xlUp).Row
  21.     '取最大列数
  22.     ColCount = Selection.Columns.Count
  23.     '新增一列用来标记是否第一行
  24.     FlagCol = ColCount + 1
  25.     'MsgBox "总行数" & RowCount & ",总列数" & ColCount & " " & ColCount + 1 & Cells(1, ColCount)
  26.     '第一行是表头,第二行才是数据
  27.     For i = 2 To RowCount
  28.         j = 1
  29.         Cells(i, ColCount + 1) = "1"
  30.         Do While True
  31.             If Cells(i, 1) = Cells(i + j, 1) Then
  32.                 Cells(i + j, FlagCol) = "0"
  33.                 ' 第3列是“转置”列
  34.                 Cells(i, FlagCol + j) = Cells(i + j, 3)
  35.             Else
  36.                 i = i + j - 1
  37.                 Exit Do
  38.             End If
  39.             j = j + 1
  40.         Loop
  41.     Next
  42.     
  43.     Columns("A:S").Select
  44.     Selection.AutoFilter
  45.     ActiveSheet.Range("$A$1:$S" & RowCount).AutoFilter Field:=FlagCol, Criteria1:="1"


  46.  End Sub
 
 


alimama_pid="mm_12187975_1642518_5895203"; alimama_titlecolor="0000FF"; alimama_descolor ="000000"; alimama_bgcolor="FFFFFF"; alimama_bordercolor="E6E6E6"; alimama_linkcolor="008000"; alimama_bottomcolor="FFFFFF"; alimama_anglesize="0"; alimama_bgpic="0"; alimama_icon="0"; alimama_sizecode="38"; alimama_width=290; alimama_height=200; alimama_type=2;
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP