|
pppeeerrr 发表于 2014-6-2 14:11
赵老师,您好,已模拟效果,请看附件↓
工作表中有多余的数据,解决办法限制取值区域:A:B:- Sub 宏1()
- Dim cnn As Object, rs As Object, SQL$, arr, i&
- Dim objWMI As Object
- Const HKEY_LOCAL_MACHINE = &H80000002
- Set objWMI = GetObject("winmgmts:\\.\root\default:StdRegProv")
- objWMI.SetDWORDValue HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\Jet\4.0\Engines\Excel", "TypeGuessRows", 200
- arr = Sheets("各类列表").[a1].CurrentRegion
- MyPath = ThisWorkbook.Path & ""
- Set cnn = CreateObject("adodb.connection")
- cnn.Open "Provider = Microsoft.Ace.Oledb.12.0;Extended Properties =Excel 12.0;Data Source =" & MyPath & arr(1, 3)
- For i = 1 To UBound(arr)
- SQL = "select * from [Excel 12.0;imex=1;Database=" & MyPath & arr(i, 3) & ";].[" & arr(2, 2) & "$a:b]"
- With Sheets(arr(i, 6))
- .Cells.ClearContents
- Set rs = cnn.Execute(SQL)
- For j = 1 To rs.Fields.Count
- .Cells(1, j) = rs.Fields(j - 1).Name
- Next
- .[a2].CopyFromRecordset rs
- With .UsedRange
- .Value = .Value
- End With
- End With
- Next
- rs.Close
- cnn.Close
- Set rs = Nothing
- Set cnn = Nothing
- End Sub
复制代码 |
|