ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 效率神器,一键搞定繁琐工作
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
让更多数据处理,一键完成 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
楼主: opiona

[求助] 关于SQL在工作簿-工作表之间的查询、排序、转置(盖楼中....)

  [复制链接]

TA的精华主题

TA的得分主题

发表于 2014-6-26 18:12 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
Mark~~~~~~~~~~~~~~~~~~~~~~

TA的精华主题

TA的得分主题

发表于 2014-6-29 14:52 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
记号,学习了           

TA的精华主题

TA的得分主题

发表于 2017-7-21 00:24 | 显示全部楼层
更正 漏了 补上 sqls +
諒橾呇 夔堆滇艘珨艘鎘ˋ   睡蚚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 = sqls + "delete from [Sheet1$] a "
        sqls = sqls + "where (a.PurchaseOrderNo,a.OrderDate) in (select PurchaseOrderNo,OrderDate from [Sheet1$] group by PurchaseOrderNo,OrderDate having count(*) > 1) "
        sqls = sqls + "and PurchaseOrderNo not in (select min(PurchaseOrderNo) from [Sheet1$] group by PurchaseOrderNo,OrderDate having count(*)>1)"
        cnn.Execute sqls
        
        sqls = ""
        sqls = sqls + "delete * from [Sheet1$] "                                                   ''      <--- all other fields, except PurchaseOrderNo --->
        sqls = 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


TA的精华主题

TA的得分主题

发表于 2017-7-21 00:30 | 显示全部楼层

更正 漏了 补上 sqls +
请教老师 能帮忙看一看吗?   如何用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 = sqls + "delete from [Sheet1$] a "
        sqls = sqls + "where (a.PurchaseOrderNo,a.OrderDate) in (select PurchaseOrderNo,OrderDate from [Sheet1$] group by PurchaseOrderNo,OrderDate having count(*) > 1) "
        sqls = sqls + "and PurchaseOrderNo not in (select min(PurchaseOrderNo) from [Sheet1$] group by PurchaseOrderNo,OrderDate having count(*)>1)"
        cnn.Execute sqls
        
        sqls = ""
        sqls = sqls + "delete * from [Sheet1$] "                                                   ''      <--- all other fields, except PurchaseOrderNo --->
        sqls = 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

TA的精华主题

TA的得分主题

发表于 2017-12-31 14:08 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
学习,学习,谢谢

TA的精华主题

TA的得分主题

发表于 2020-2-25 09:42 | 显示全部楼层
记号 学习,谢谢楼主
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-11-5 23:37 , Processed in 0.029154 second(s), 6 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表