|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
Option Explicit
Sub a()
Dim cat, strconn As String, tb1, tb2
Dim myf As String
Application.ScreenUpdating = False
myf = ThisWorkbook.Path & "\a.mdb"
If Dir(myf) <> "" Then Kill myf
Set cat = CreateObject("adox.catalog")
strconn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & myf
cat.Create strconn
Set tb1 = CreateObject("ADOX.Table")
Set tb2 = CreateObject("ADOX.Table")
With tb1
.ParentCatalog = cat
.Name = "T1"
.Columns.Append "ID", 3
.Columns("ID").Properties("AutoIncrement") = True
.Columns.Append "编号", 202, 50
.Keys.Append "PrimaryKey", 1, "ID"
cat.Tables.Append tb1
End With
With tb2
.ParentCatalog = cat
.Name = "T2"
.Columns.Append "ID", 3
.Columns("ID").Properties("AutoIncrement") = True
.Columns.Append "编号", 202, 50
.Keys.Append "PrimaryKey", 1, "ID"
cat.Tables.Append tb2
End With
Set cat.ActiveConnection = Nothing
Dim myfile As String, wb As Workbook, i&, J&, m&, arr, cnn, sql$
myf = ThisWorkbook.Path & "\"
myfile = Dir(myf & "*订单*.xls*")
Set cnn = CreateObject("ADODB.CONNECTION")
cnn.Open "Provider=Microsoft.jet.OLEDB.4.0;" & "Data Source=" & ThisWorkbook.Path & "\a.mdb"
Do While myfile <> ""
If myfile <> ThisWorkbook.Name Then
Set wb = Workbooks.Open(ThisWorkbook.Path & "\" & myfile)
If wb.Sheets.Count > 1 Then
For i = 1 To wb.Sheets.Count
arr = wb.Sheets(i).[A1].CurrentRegion
For J = 1 To 5 Step 4
For m = 1 To UBound(arr)
sql = "INSERT INTO T2(编号) VALUES('" & arr(m, J) & "')"
cnn.Execute sql
Next
Next
Next
Else
arr = wb.Sheets(1).[A1].CurrentRegion
For m = 2 To UBound(arr)
sql = "INSERT INTO T1(编号) VALUES('" & arr(m, 1) & "')"
cnn.Execute sql
Next
End If
wb.Close 0
End If
myfile = Dir
Loop
With Sheet2
.Cells.Clear
.[A1] = "ERP有但SRM找不到"
.[D1] = "SRM有但ERP查不到"
sql = "select 编号 FROM T1 WHERE 编号 NOT IN (SELECT 编号 from T2)"
.Range("B2").CopyFromRecordset cnn.Execute(sql)
sql = "select 编号 FROM T2 WHERE 编号 NOT IN (SELECT 编号 from T1)"
.Range("E2").CopyFromRecordset cnn.Execute(sql)
End With
Set cnn = Nothing
Application.ScreenUpdating = True
End Sub
|
|