|
本帖最后由 baofa2 于 2024-4-17 18:23 编辑
需要出问题的附件。
楼下附件……
楼下附件并无不妥测试成功.zip
(292.67 KB, 下载次数: 11)
- Option Explicit
- '哎,随便更改表名及工作表数量…… 适用于 11F 附件
- Sub test1() '无花,真不愿占用新楼层,但愿你能看到……
-
- ActiveSheet.UsedRange.Offset(1).Clear
- Application.ScreenUpdating = False
-
- Dim p As String, f As String
- Dim ar, br, dic As Object, dict As Object, target As Range
- Dim Conn As Object, rs As Object, Cata As Object, tb As Object, str_ As String
- Dim strConn As String, SQL As String, s As String, t As String, n As String, i As Long
-
- Set target = Range("A2")
- Set dic = CreateObject("Scripting.Dictionary")
- Set dict = CreateObject("Scripting.Dictionary")
- Set Cata = CreateObject("ADOX.Catalog")
- Set Conn = CreateObject("ADODB.Connection")
-
- ar = Application.Rept(Range("A1").CurrentRegion.Rows(1).Value, 1)
- For i = 1 To UBound(ar)
- s = "`" & Trim(ar(i)) & "`"
- dic.Add s, i
- ar(i) = "NULL AS " & s
- Next
-
- s = "Excel 12.0;HDR=YES;IMEX=1;Database="
- If Application.Version < 12 Then
- s = Replace(s, "12.0", "8.0")
- strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties='Excel 8.0;HDR=YES';Data Source="
- Else
- strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties='Excel 12.0;HDR=YES';Data Source="
- End If
- Conn.Open strConn & ThisWorkbook.FullName
-
- p = ThisWorkbook.Path & "\"
- f = Dir(p & "*.xls*")
- While Len(f)
- If p & f <> ThisWorkbook.FullName Then
- str_ = ""
- If InStr(f, "发货清单,") Then str_ = Replace(Split(f, "202")(0), "发货清单,", "")
- Cata.ActiveConnection = strConn & p & f
- For Each tb In Cata.Tables
- If tb.Type = "TABLE" Then
- t = Replace(tb.Name, "'", vbNullString)
- If Right(t, 1) = "$" Then
- If InStr(t, "发货清单") Then
- br = ar
- SQL = "SELECT * FROM [" & s & p & f & "].[" & t & "A6:AA6] WHERE FALSE"
- Set rs = Conn.Execute(SQL)
- For i = 0 To rs.Fields.Count - 1
- n = "`" & rs.Fields(i).Name & "`"
- If dic.Exists(n) Then br(dic(n)) = n
- Next
- If str_ = "" Then str_ = Replace(Split(t, "202")(0), "发货清单,", "")
- br(1) = Replace(br(1), "NULL", "'" & str_ & "'")
- SQL = "SELECT " & Join(br, ",") & " FROM [" & s & p & f & "].[" & t & "A6:AA] WHERE LEN(石种)>0"
- dict.Add SQL, vbNullString
- If dict.Count = 49 Then
- target.CopyFromRecordset Conn.Execute(Join(dict.Keys, " UNION ALL "))
- Set target = Range("A" & Cells.Find("*", , xlValues, , xlByRows, xlPrevious).Row + 1)
- dict.RemoveAll
- End If
- 'Exit For
- End If
- End If
- End If
- Next
- End If
- f = Dir
- Wend
- If dict.Count Then target.CopyFromRecordset Conn.Execute(Join(dict.Keys, " UNION ALL "))
-
- With Range("A1").CurrentRegion
- .Borders.LineStyle = xlContinuous
- .HorizontalAlignment = xlCenter
- .Font.Name = "微软雅黑"
- .Font.Size = 10
- .Value = .Value
- End With
-
- rs.Close
- Set rs = Nothing
- Conn.Close
- Set Conn = Nothing
- Set Cata = Nothing
- Set target = Nothing
- Set dic = Nothing
- Set dict = Nothing
- Application.ScreenUpdating = True
- Beep
- End Sub
复制代码
|
|