实例10:对于大数量文件,不打开源文件及目标文件而进行重新分类 http://club.excelhome.net/viewthread.php?tid=170735&px=0 Sub make() Dim Sql$, countt%, i% Dim Filename As Variant
Filename = Application.GetOpenFilename("Microsoft Office Excel Files (*.xls), *.xls", , "请选取文件", , MultiSelect:=True) If Not IsArray(Filename) Then Exit Sub
Set cn = CreateObject("ADODB.Connection") '(1) countt = 0 For Each fn In Filename countt = countt + 1 cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=Excel 8.0;Data Source=" & fn '(2) For i = 2 To [A2].End(xlDown).Row '每打开一个文件即遍历所有型号,将所有型号对应记录写入相应文件中 If countt = 1 Then '打开第一个文件时产生新分类文件,且写入字段名及所选记录 Sql = "SELECT * INTO [Sheet1] IN '" & ThisWorkbook.Path & "\" & Cells(i, 1) & ".xls' 'Excel 8.0;' FROM [sheet1$] where 型号=" & Cells(i, 1) '其中.xls' 'Excel之间空格不可缺 (3) cn.Execute Sql '(4) Else '打开文件为非第一个时则直接将所选记录插入到已产生的分类文件尾部 Sql = "INSERT INTO [Sheet1$] IN '" & ThisWorkbook.Path & "\" & Cells(i, 1) & ".xls' 'Excel 8.0;' SELECT * FROM [sheet1$] where 型号=" & Cells(i, 1) cn.Execute Sql End If Next cn.Close '(5) Next Set cn = Nothing '(6) End Sub 使用 “ SELECT * INTO ”语句产生新文件以及使用 “ INSERT INTO .... IN ”语句进行不打开文件的插入时,均要求字段名,而实际处理大量文件时,往往字段名无法做到完全相同,所以碰到此种情况时,常需辅以打开文件的操作。
[此贴子已经被作者于2006-10-12 13:30:25编辑过] |