|
是文件数超过18个?确切地说是工作表数超过49个,如果是需要分步复制数据,请测试:- Sub Macro1()
- '引用Microsoft AD0 Ext 2.8 for DDL and Security
- '引用Microsoft ActiveX Data Objects 2.x Library
- Dim cnn As New ADODB.Connection
- Dim rs As ADODB.Recordset
- Dim cat As New ADOX.Catalog, tb1 As Table
- Dim d As Object, ds As Object
- Dim SQL$, MyFile$, m%, i%, temp$, strField$, s$, t$, t2$, n%
- Application.ScreenUpdating = False
- Set d = CreateObject("scripting.dictionary")
- Set ds = CreateObject("scripting.dictionary")
- Sheets("项目取数").[a1].CurrentRegion.Offset(3).ClearContents
- Sheets("基础信息").[a1].CurrentRegion.Offset(3).ClearContents
- Mypath = ThisWorkbook.Path & ""
- MyFile = Dir(Mypath & "*.xlsx")
- Do While MyFile <> ""
- If MyFile <> ThisWorkbook.Name Then
- n = n + 1
- If n > 1 Then
- t = "[Excel 12.0;Database=" & Mypath & MyFile & "]."
- Else
- cnn.Open "Provider=Microsoft.Ace.OLEDB.12.0;Extended Properties=Excel 12.0;Data Source=" & Mypath & MyFile
- End If
- t2 = "[Excel 12.0;HDR=No;Database=" & Mypath & MyFile & "]."
- cat.ActiveConnection = "Provider=Microsoft.Ace.OLEDB.12.0;Extended Properties='Excel 12.0;HDR=No';Data Source=" & Mypath & MyFile
- For Each tb1 In cat.Tables
- If tb1.Type = "TABLE" Then
- s = Replace(tb1.Name, "'", "")
- If Right(s, 1) = "$" Then
- m = m + 1
- If m = 1 Then
- Set rs = cnn.Execute("[" & s & "a5:d]")
- For i = 0 To rs.Fields.Count - 1
- temp = temp & rs.Fields(i).Name & ","
- Next
- End If
- SQL = "select " & temp & "'" & Replace(s, "$", "") & "' as 工作表,'" & Replace(MyFile, ".xlsx", "") & "' as 工作簿 from " & t & "[" & s & "a5:d]"
- d(SQL) = ""
- SQL2 = "select " & "'" & Replace(MyFile, ".xlsx", "") & "' as 工作簿,'" & Replace(s, "$", "") & "' as 工作表,(select f1 from " & t2 & "[" & s & "b1:b1]),(select f1 from " & t2 & "[" & s & "b3:b3]),(select f1 from " & t2 & "[" & s & "e1:e1]) from " & t2 & "[" & s & "a1:e1]"
- ds(SQL2) = ""
- If m Mod 49 = 0 Then Call Replicated_data(d, ds, cnn)
- End If
- End If
- Next
- End If
- MyFile = Dir()
- Loop
- If d.Count > 0 Then Call Replicated_data(d, ds, cnn)
- rs.Close
- cnn.Close
- Set rs = Nothing
- Set cnn = Nothing
- Set cat = Nothing
- Set tb1 = Nothing
- Application.ScreenUpdating = True
- End Sub
- Sub Replicated_data(ByRef d As Object, ByRef ds As Object, ByRef cnn As Object)
- Dim SQL$
- SQL = Join(d.Keys, " UNION ALL ")
- Sheets("项目取数").[a65536].End(3).Offset(1).CopyFromRecordset cnn.Execute(SQL)
- SQL = Join(ds.Keys, " UNION ALL ")
- Sheets("基础信息").[a65536].End(3).Offset(1).CopyFromRecordset cnn.Execute(SQL)
- d.RemoveAll
- ds.RemoveAll
- End Sub
复制代码 |
评分
-
1
查看全部评分
-
|