VBA初试——合并

今天遇到一个任务,要把excel中的相同行合并;没有找到直接的办法(vlookup大概能做),试了试VBA挺好用

数据结构

第一、二列是描述字段,第二列元素唯一;第三到第八列是标志位

任务

合并描述字段相同的行,描述字段不变,标志位合并

方法:

1.录制空宏(命名为test1),查看宏进入VBA编辑器模式

2.代码

Sub test1()Dim i As IntegerDim j As Integeri = 2 '跳过标题行Do While ActiveSheet.Cells(i, 2).Value <> ""If ActiveSheet.Cells(i, 2).Value = ActiveSheet.Cells(i + 1, 2).Value Then    For j = 3 To 8 Step 1        ActiveSheet.Cells(i, j).Value = ActiveSheet.Cells(i, j).Value & ActiveSheet.Cells(i + 1, j).Value    Next        ActiveSheet.Rows(i + 1).Delete '循环删除Else    i = i + 1End IfLoopEnd Sub

3.在所需工作表(即ActiveSheet)执行宏

4.保存为.xlsm文件并启用宏

参考资料:

http://blog.csdn.net/sunxing007/article/details/5658384

http://blog.sina.com.cn/s/blog_7f14264a01016ssb.html

https://msdn.microsoft.com/zh-cn/library/office/ee814737.aspx

https://msdn.microsoft.com/en-us/library/office/ee861528.aspx

到尽头,也许快乐,或有时孤独,如果心在远方,

VBA初试——合并

相关文章:

你感兴趣的文章:

标签云: