|
发表于 2020-10-22 13:10
来自手机
|
显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
Private Sub CommandButton1_Click()
Dim qt As QueryTable, sh As Worksheet
Dim wbname As Name, wbnames As Names
Dim i As Integer
Set wbnames = ThisWorkbook.Names
For Each wbname In wbnames
wbname.Delete
Next
ThisWorkbook.Names.Add "数据区域", ThisWorkbook.Sheets(1).Range("a3", "h" & ThisWorkbook.Sheets(1).[a1048576].End(xlUp).Row)
Set sh = ThisWorkbook.Sheets(2)
sh.Cells.Delete
Set qt = sh.QueryTables.Add("ODBC;DSN=Excel Files;DBQ=" & ThisWorkbook.FullName, sh.[a1])
With qt
.CommandText = "select 数据区域.*, DateDiff('d',开单日期,Now()) as 距今天数 from (数据区域 inner join (SELECT 收货人,Max(开单日期) as 日期,发货人 FROM 数据区域 group by 收货人,发货人) as b on 数据区域.收货人=b.收货人 and 数据区域.开单日期=b.日期 and 数据区域.发货人=b.发货人) where datediff('d',开单日期,now()) > " & ThisWorkbook.Sheets(1).[j1].Value & " order by 数据区域.开单日期"
.Refresh
End With
On Error Resume Next
For i = 0 To ThisWorkbook.Connections.Count
ThisWorkbook.Connections(i).Delete
Next
End Sub |
|