|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
原帖由 sl8831 于 2011-1-14 10:33 发表
劳驾高手 再帮忙看看~~
高手 太感谢了 对于这个代码还有三个请求
一:能否让筛好的表打开后显示在表头位置,现在一打开是在最下面 还要自己往上拉。同时表下面有部分空白表格自己增加了边框 能否完善一下。
二 ...
Sub Macro1()
Dim cnn As Object, rs As Object, wb As Workbook, wb1 As Workbook
Dim SQL$, arr, i%, desk$, Filename, sh As Worksheet, d As Object, av%, s$
av = Application.Version
If av <= 11 Then
Filename = Application.GetOpenFilename(FileFilter:="Excel 工作簿文件 (*.xls),*.xls", Title:="请选择文件")
s = ".xls"
Else
Filename = Application.GetOpenFilename(FileFilter:="Excel 工作簿文件 (*.xls;*.xlsx;*.xlsm),*.xls;*.xlsx;*.xlsm", Title:="请选择文件")
s = ".xlsx"
End If
If TypeName(Filename) = "Boolean" Then Exit Sub
If Filename = ThisWorkbook.FullName Then
MsgBox "不能选择本文件!请重新选择"
Exit Sub
End If
arr = Array("河南郑州", "河南洛阳", "河南新乡", "福建福州", "福建厦门", "福建泉州", "安徽合肥")
Set d = CreateObject("scripting.dictionary")
If Val(Right(Application.OperatingSystem, 4)) >= 6 Then
desk = Environ("USERPROFILE") & "\Desktop\"
Else
desk = Environ("USERPROFILE") & "\桌面\"
End If
Set cnn = CreateObject("ADODB.Connection")
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Set wb = Workbooks.Open(Filename)
If av <= 11 Then
cnn.Open "provider=Microsoft.Jet.OLEDB.4.0;extended properties=excel 8.0;data source=" & Filename
Else
cnn.Open "Provider=Microsoft.Ace.OLEDB.12.0;Extended Properties=Excel 12.0;Data Source=" & Filename
End If
For Each sh In wb.Sheets
If Len(SQL) Then SQL = SQL & " union all "
SQL = SQL & "Select distinct 地区 From [" & sh.Name & "$]"
Next
Set rs = CreateObject("ADODB.Recordset")
rs.Open SQL, cnn, 1, 3
For i = 1 To rs.RecordCount
d(rs.Fields(0).Value) = ""
rs.MoveNext
Next
For i = 0 To UBound(arr)
If d.Exists(arr(i)) Then
wb.SaveCopyAs desk & arr(i) & s
Set wb1 = Workbooks.Open(desk & arr(i) & s)
For Each sh In wb1.Sheets
SQL = "Select * From [" & sh.Name & "$] Where 地区='" & arr(i) & "'"
Set rs = CreateObject("ADODB.Recordset")
rs.Open SQL, cnn, 1, 3
If rs.RecordCount > 0 Then
With sh
.Activate
.[a1].Select
lr = .[a1].CurrentRegion.Rows.Count
.[a1].CurrentRegion.Offset(1 + rs.RecordCount).Clear
.[a2].CopyFromRecordset rs
End With
ActiveWindow.SmallScroll Down:=lr * (-1)
Else
sh.Delete
End If
Next
wb1.Close True
End If
Next
wb.Close False
rs.Close
cnn.Close
Set rs = Nothing
Set cnn = Nothing
Application.DisplayAlerts = True
Application.ScreenUpdating = True
MsgBox "ok"
End Sub |
|