|
yangjkab 发表于 2013-9-17 16:22
大侠。你太厉害了。测试正确。能否写一些备注,给代码解释一下。。我是个初学者,有点看不懂这个高深的代 ...
原理很简单,主要是字典应用:- Sub Macro1()
- Dim arr, brr, d As Object, k, t, i&, j&, m&, l&
- Application.ScreenUpdating = False
- Application.DisplayAlerts = False
- Set d = CreateObject("scripting.dictionary") '创建字典对象
- arr = [a1].CurrentRegion '数据区域写入数组
- For i = 2 To UBound(arr) '逐行
- d(arr(i, 1)) = d(arr(i, 1)) & "," & i '编码和行号关联
- Next
- k = d.Keys '字典键值数组
- brr = arr '数组brr赋值,同时把arr中的标题行传递到brr
- For i = 0 To d.Count - 1 '逐个编码
- m = 1 '保留标题行
- t = Split(d(k(i)), ",") '每个编号所对应的行号
- For j = 1 To UBound(t) '逐个行号
- m = m + 1 '计数
- For l = 1 To UBound(arr, 2) '逐行
- brr(m, l) = arr(t(j), l) '该编码对应的行数据写入brr
- Next
- Next
- With Workbooks.Add(xlWBATWorksheet) '新建一个工作簿
- .Sheets(1).[a1].Resize(m, UBound(arr, 2)) = brr '写数据
- .SaveAs ThisWorkbook.Path & "" & k(i) & ".xlsx" '保存文件
- .Close '关闭
- End With
- Next
- Application.DisplayAlerts = True
- Application.ScreenUpdating = True
- MsgBox "OK"
- End Sub
复制代码 |
评分
-
1
查看全部评分
-
|