|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
Sub Macro1()
Dim Fso As Object, File As Object, cnn As Object, SQL$, arr, arrf$(), i&, j&, l&, u&, v&, n&, d As Object
Set Fso = CreateObject("Scripting.FileSystemObject")
ReDim arrf(1 To Fso.GetFolder(ThisWorkbook.Path).Files.Count)
For Each File In Fso.GetFolder(ThisWorkbook.Path).Files
If File.Name Like "发货记录表*.xlsx" Then
n = n + 1
arrf(n) = File
End If
Next
Set d = CreateObject("scripting.dictionary")
Set cnn = CreateObject("adodb.connection")
cnn.Open "Provider=Microsoft.ace.OLEDB.12.0;;Data Source=" & ThisWorkbook.Path & "\Database.accdb"
For l = n To 1 Step -1
SQL = "select f3 as 订单号,f9 as 发货日期,f7 as 发往 from [Excel 12.0;hdr=no;imex=1;Database=" & arrf(l) & ";].[发货$a6:j] WHERE f10='临时'"
Set rs = cnn.Execute(SQL)
If Not rs.EOF Then
arr = rs.GetRows
For i = 0 To UBound(arr, 2)
If Not d.Exists(arr(0, i)) Then
d(arr(0, i)) = ""
SQL = "select * from 临时 where [Order]='" & arr(0, i) & "'"
MsgBox SQL
Set rs = CreateObject("adodb.Recordset")
rs.Open SQL, cnn, 1, 3
If rs.RecordCount = 0 Then
rs.AddNew
u = u + 1
Else
v = v + 1
End If
For j = 0 To rs.Fields.Count - 1
rs.Fields(j) = arr(j, i)
Next
rs.Update
End If
Next
End If
Next
MsgBox "添加" & u & "条记录,更新" & v & "条记录。", vbInformation
rs.Close
cnn.Close
Set rs = Nothing
Set cnn = Nothing
Set Fso = Nothing
End Sub |
|