|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
请教老师 能帮房看一看吗? 如何用EXCEL VBA SQL语句删除表中多余的重复记录(多个字段)只保留一条
Option Explicit
Dim cnn As New ADODB.Connection
Dim rst As New ADODB.Recordset
Dim sqls As String
Dim i As Integer
Sub SQLDELEDUPLICATERECORD()
'' Excel VBA SQL 删除表中多余的重复记录多个字段)只保留一条
SQLTEST = "SQLTEST.xls"
Workbooks(SQLTEST).Worksheets(1).Activate
MYPath = "D:\Users\Administrator\Documents\"
DBSFullName = MYPath + SQLTEST
If cnn.State = adStateOpen Then
cnn.Close
Set cnn = Nothing
End If
cnn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=Excel 8.0;Data Source=" & DBSFullName ''ThisWorkbook.FullName
If cnn.State = adStateOpen Then
'' 以下两种 SQL语句都失败 请教如何修改
sqls = ""
sqls = "delete from [Sheet1$] a "
sqls = "where (a.PurchaseOrderNo,a.OrderDate) in (select PurchaseOrderNo,OrderDate from [Sheet1$] group by PurchaseOrderNo,OrderDate having count(*) > 1) "
sqls = "and PurchaseOrderNo not in (select min(PurchaseOrderNo) from [Sheet1$] group by PurchaseOrderNo,OrderDate having count(*)>1)"
cnn.Execute sqls
sqls = ""
sqls = "delete * from [Sheet1$] " '' <--- all other fields, except PurchaseOrderNo --->
sqls = "where PurchaseOrderNo NOT in (select min(PurchaseOrderNo) from [Sheet1$] group by IDNo,OrderDate,OrderFirmCode,BuyerName,BuyerEmail,SupplierNo,PaymentTermsNET,FreightTerms,Transportation,ShipVia,Destination)" ''' having count(*)>1
cnn.Execute sqls
End If
If cnn.State = adStateOpen Then
cnn.Close
Set cnn = Nothing
End If
If rst.State Then rst.Close
If rst.State = 1 Then rst.Close
Set rst = Nothing
End Sub
ROBERTCHENTPE
|
|