将excel按照某一列拆分成多个文件(方案整理)

 

1解决方案:将excel按照某一列拆分成多个文件

百度得:https://blog.csdn.net/ntotl/article/details/79141314

2遇到问题:解决vbe6ext.olb不能被加载 内存溢出 问题

百度得:http://www.udaxia.com/wtjd/13635.html

3问题原因:VBA文件位置不同

有的是:C:\Program Files (x86)\Common Files\microsoft shared\VBA

有的是:C:\Program Files \Common Files\microsoft shared\VBA

而我两个位置都有VBA文件。所以我先把两个地方的VBA文件复制一份(怕搞错无法恢复),然后把两个地方的VBA6、VBA7.1 都试了一篇,最后成功了。

4VB核心代码:

Sub 保留表头拆分数据为若干新工作簿()
 Dim arr, d As Object, k, t, i&, lc%, rng As Range, c%
 c = Application.InputBox("请输入拆分列号", , 4, , , , , 1)
 If c = 0 Then Exit Sub
Application.ScreenUpdating = False
Application.DisplayAlerts = False
arr = [a1].CurrentRegion
lc = UBound(arr, 2)
Set rng = [a1].Resize(, lc)
 Set d = CreateObject("scripting.dictionary")
For i = 2 To UBound(arr)
If Not d.Exists(arr(i, c)) Then
Set d(arr(i, c)) = Cells(i, 1).Resize(1, lc)
 Else
 Set d(arr(i, c)) = Union(d(arr(i, c)), Cells(i, 1).Resize(1, lc))
End If
 Next
k = d.Keys
 t = d.Items
For i = 0 To d.Count - 1
With Workbooks.Add(xlWBATWorksheet)
rng.Copy .Sheets(1).[a1]
 t(i).Copy .Sheets(1).[a2]
 .SaveAs Filename:=ThisWorkbook.Path & "\" & k(i) & ".xls"
 .Close
 End With
 Next
 Application.DisplayAlerts = True
 Application.ScreenUpdating = True
 MsgBox "完毕"
End Sub
View Code

 

转载于:https://www.cnblogs.com/hao-1234-1234/p/10530027.html

你可能感兴趣的:(将excel按照某一列拆分成多个文件(方案整理))